Get multiple connection strings in appsettings.json without EF

Just started playing with .Net Core RC2, porting the current MVC.Net application that I developed. It seems to me that due to the way the configuration is handled using appsettings.json, if I have multiple connection strings, I either have to use EF to retrieve the connection string, or I need to create separate classes whose names are given for each connection string . All the examples that I see either use EF (which does not make sense to me, since I will use Dapper), or the class creates a class with the name after the section in config. Is there a better solution?

    "Data": {
        "Server1": {
          "ConnectionString": "data source={server1};initial catalog=master;integrated security=True;"
        },
        "Server2": {
          "ConnectionString": "data source={server2};initial catalog=master;integrated security=True;"
        }
    }

Why would I want to build two classes, one of which is called "Server1" and another "Server2", if each had a connection?

+7
source share
3 answers

There are several corrections that I made to answer Adem to work with RC2, so I decided it was better to place them.

I set up appsettings.json and create a class like Adem's

{
    "ConnectionStrings": {
      "DefaultConnectionString": "Default",
      "CustomConnectionString": "Custom"
    }
}

and

public class ConnectionStrings
{
    public string DefaultConnectionString { get; set; }

    public string CustomConnectionString { get; set; }
}

most of the Adem code goes out of the box in VS for RC2, so I just added the line below to the ConfigureServices method

services.Configure<Models.ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));

, ( , , Controller Action, IOptions, https://docs.asp.net/en/latest/mvc/controllers/dependency-injection.html)

, ,

private readonly ConnectionStrings _connectionStrings;
        public HomeController(IOptions<ConnectionStrings> connectionStrings)
        {
            _connectionStrings = connectionStrings.Value;
        }

, DAL, connectionString

DAL.DataMethods dm = new DAL.DataMethods(_connectionStrings.CustomConnectionString);

, , DAL .

+3

Options DAL. (RC1):

appsettings.json :

{
    "ConnectionStrings": {
      "DefaultConnectionString": "Default",
      "CustomConnectionString": "Custom"
    }
}

:

public class ConnectionStrings
{
    public string DefaultConnectionString { get; set; }

    public string CustomConnectionString { get; set; }
}

Startup.cs

    private IConfiguration Configuration;
    public Startup(IApplicationEnvironment app)
    {
        var builder = new ConfigurationBuilder()
           .SetBasePath(app.ApplicationBasePath)
           .AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }
    public void ConfigureServices(IServiceCollection services)
    {
        // ....
        services.AddOptions();
        services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
    }

, DAL:

    private IOptions<ConnectionStrings> _connectionStrings;
    public DalClass(IOptions<ConnectionStrings> connectionStrings)
    {
        _connectionStrings = connectionStrings;
    }
    //use it
0

I don't like the idea of ​​creating a DAL. Rather, I would do something like this

public class ConnectionStrings : Dictionary<string, string> { }

And something like that in ctor of DAL

public Dal(IOptionsMonitor<ConnectionStrings> optionsAccessor, ILogger<Dal> logger)
{
      _connections = optionsAccessor.CurrentValue;
      _logger = logger;
}

Now you have all the connection strings in the DAL object. You can use them in every query, or even select it by index with every call.

0
source

All Articles