Get Multiple Connection Strings in appsettings.json without EF

前端 未结 3 764
天命终不由人
天命终不由人 2021-01-12 08:08

Just starting playing with the .Net Core RC2 by migrating a current MVC .Net app I developed. It looks like to me because of the way that configuration is handled with apps

3条回答
  •  感情败类
    2021-01-12 08:27

    I don't like the idea of instantiating the DAL. Rather, I'd do something like this

    public class ConnectionStrings : Dictionary { }
    

    And something like this in the ctor of the DAL

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

    and you'll need to register with IoC

        services.Configure(configuration.GetSection("ConnectionStrings")); /* services is the IServiceCollection */
    

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

提交回复
热议问题