你的位置:首页 > ASP.net教程

[ASP.net教程].NET Core采用的全新配置系统[7]: 将配置保存在数据库中


我们在《聊聊默认支持的各种配置源》和《深入了解三种针对文件(JSON、

目录
一、在应用中使用自定义的DbConfigurationSource
二、ApplicationSetting & ApplicationSettingsContext
三、DbConfigurationSource
四、DbConfigurationProvider
五、扩展方法AddDatabase

一、在应用中使用自定义的DbConfigurationSource

我们将这个自定义ConfigurationSource命名为DbConfigurationSource。在正式对它的实现展开介绍之前,我们先来看看它在项目中的应用。我们创建一个控制台程序来演示对这个DbConfigurationSource应用。我们将配置保存在SQL Server数据库中的某个数据表中,并采用Entity Framework Core来读取配置,所以我们需要添加针对“ Microsoft.EntityFrameworkCore”和“Microsoft.EntityFrameworkCore.SqlServer”这两个NuGet包的依赖。除此之外,我们的实例程序会采用Options模式将读取的配置绑定为了一个Options对象,所以我们添加了针对NuGet包“Microsoft.Extensions.DependencyInjection”和“Microsoft.Extensions.Options.ConfigurationExtensions”的依赖。

  1: {
  2:  ...
  3:  "buildOptions": {
  4:   ...
  5:   "copyToOutput": "connectionString.json"
  6:  },
  7:  
  8:  "dependencies": {
  9:   ...
 10:   "Microsoft.Extensions.Options.ConfigurationExtensions"  : "1.0.0",
 11:   "Microsoft.Extensions.DependencyInjection"        : "1.0.0",
 12:   "Microsoft.Extensions.Configuration.Json"         : "1.0.0",
 13:   "Microsoft.EntityFrameworkCore.SqlServer"         : "1.0.0",
 14:   "Microsoft.EntityFrameworkCore"              : "1.0.0"
 15:  } 
 16: }


我们将链接字符串作为配置定义在一个名为“connectionString.json”的JSON文件中,所以我们添加了针对NuGet包“Microsoft.Extensions.Configuration.Json”的依赖。链接字符串采用如下的形式定义在这个JSON文件中的定义,我们修改了“buildOptions/copyToOutput”配置项使这个文件可以在编译的时候可以自动拷贝到输出目录下。

  1: {
  2:  "connectionStrings": {
  3:   "defaultDb": "Server = ... ; Database=...; Uid = ...; Pwd = ..."
  4:  }
  5: }


我们编写了如下的程序来演示针对自定义ConfigurationSource(DbConfigurationSource)的应用。我们首先创建了一个ConfigurationBuilder对象,并注册了一个指向“connectionString.json”文件的JsonConfigurationSource。针对DbConfigurationSource的注册体现在扩展方法AddDatabase上,这个方法接收两个参数,它们分别代表链接字符串的名称和初始的配置数据。前者正式“connectionString.json”设置的连接字符串名称“defaultDb”,后者是一个字典对象,它提供的原始配置正好可以构成一个Profile对象。在利用ConfigurationBuilder创建出相应的Configuration对象之后,我们采用标准的Options编程模式读取配置将将其绑定为一个Profile对象。

  1: var initialSettings = new Dictionary<string, string>
  2: {
  3:   ["Gender"]       = "Male",
  4:   ["Age"]         = "18",
  5:   ["ContactInfo:EmailAddress"]   = "foobar@outlook.com",
  6:   ["ContactInfo:PhoneNo"]     = "123456789"
  7: };
  8:  
  9: IConfiguration config = new ConfigurationBuilder()
 10:   .AddJsonFile("connectionString.json")
 11:   .AddDatabase("DefaultDb", initialSettings)
 12:   .Build();
 13:  
 14: Profile profile = new ServiceCollection()
 15:   .AddOptions()
 16:   .Configure<Profile>(config)
 17:   .BuildServiceProvider()
 18:   .GetService<IOptions<Profile>>()
 19:   .Value;
 20:  
 21: Debug.Assert(profile.Gender == Gender.Male);
 22: Debug.Assert(profile.Age == 18);
 23: Debug.Assert(profile.ContactInfo.EmailAddress == "foobar@outlook.com");
 24: Debug.Assert(profile.ContactInfo.PhoneNo == "123456789");
 25:  
 26:  
 27: public class Profile
 28: {
 29:   public Gender     Gender { get; set; }
 30:   public int      Age { get; set; }
 31:   public ContactInfo  ContactInfo { get; set; }
 32: }
 33:  
 34: public class ContactInfo
 35: {
 36:   public string EmailAddress { get; set; }
 37:   public string PhoneNo { get; set; }
 38: }
 39:  
 40: public enum Gender
 41: {
 42:   Male,
 43:   Female
 44: }
 45:  



二、ApplicationSetting & ApplicationSettingsContext

如上面的代码片断所示,针对DbConfigurationSource的应用仅仅体现在我们为ConfigurationBuilder定义的扩展方法AddDatabase上,所以使用起来是非常方便的,那么这个扩展方法背后有着怎样的逻辑实现呢?DbConfigurationSource采用Entity Framework Core以Code First的方式进行数据操作,如下所示的ApplicationSetting是表示基本配置项的POCO类型,我们将配置项的Key以小写的方式存储。另一个ApplicationSettingsContext是对应的DbContext类型。

  1: [Table("ApplicationSettings")]
  2: public class ApplicationSetting
  3: {
  4:   private string key;
  5:  
  6:   [Key]
  7:   public string Key
  8:   {
  9:     get { return key; }
 10:     set { key = value.ToLowerInvariant(); }
 11:   }
 12:  
 13:   [Required]
 14:   [MaxLength(512)]
 15:   public string Value { get; set; }
 16:  
 17:   public ApplicationSetting()
 18:   {}
 19:  
 20:   public ApplicationSetting(string key, string value)
 21:   {
 22:     this.Key   = key;
 23:     this.Value  = value;
 24:   }
 25: }
 26:  
 27: public class ApplicationSettingsContext : DbContext
 28: {
 29:   public ApplicationSettingsContext(DbContextOptions options) : base(options)
 30:   {}
 31:  
 32:   public DbSet<ApplicationSetting> Settings { get; set; }
 33: }



三、DbConfigurationSource

如下所示的是DbConfigurationSource的定义,它的构造函数接受两个参数,第一个参数类型为Action<DbContextOptionsBuilder>的委托对象,我们用它来对创建DbContext采用的DbContextOptions进行设置,另一个可选的参数用来指定一些需要自动初始化的配置项。DbConfigurationSource在重写的Build方法中利用这两个对象创建一个DbConfigurationProvider对象。

  1: public class DbConfigurationSource : IConfigurationSource
  2: {
  3:   private Action<DbContextOptionsBuilder> _setup;
  4:   private IDictionary<string, string> _initialSettings;
  5:  
  6:   public DbConfigurationSource(Action<DbContextOptionsBuilder> setup, IDictionary<string, string> initialSettings = null)
  7:   {
  8:     _setup        = setup;
  9:     _initialSettings   = initialSettings;
 10:   }
 11:   public IConfigurationProvider Build(IConfigurationBuilder builder)
 12:   {
 13:     return new DbConfigurationProvider(_setup, _initialSettings);
 14:   }
 15: }



四、DbConfigurationProvider

DbConfigurationProvider派生于抽象类ConfigurationProvider。在重写的Load方法中,它会根据提供的Action<DbContextOptionsBuilder>创建ApplicationSettingsContext对象,并利用后者从数据库中读取配置数据并转换成字典对象并赋值给代表配置字典的Data属性。如果数据表中没有数据,该方法还会利用这个DbContext对象将提供的初始化配置添加到数据库中。

  1: public class DbConfigurationProvider: ConfigurationProvider
  2: {
  3:   private IDictionary<string, string>     _initialSettings;
  4:   private Action<DbContextOptionsBuilder>   _setup;
  5:  
  6:   public DbConfigurationProvider(Action<DbContextOptionsBuilder> setup, IDictionary<string, string> initialSettings)
  7:   {
  8:     _setup        = setup;
  9:     _initialSettings   = initialSettings?? new Dictionary<string, string>() ;
 10:   }
 11:  
 12:   public override void Load()
 13:   {
 14:     DbContextOptionsBuilder<ApplicationSettingsContext> builder = new DbContextOptionsBuilder<ApplicationSettingsContext>();
 15:     _setup(builder);
 16:     using (ApplicationSettingsContext dbContext = new ApplicationSettingsContext(builder.Options))
 17:     {
 18:       dbContext.Database.EnsureCreated();
 19:       this.Data = dbContext.Settings.Any()? dbContext.Settings.ToDictionary(it => it.Key, it => it.Value, StringComparer.OrdinalIgnoreCase): this.Initialize(dbContext);
 20:     }
 21:   }
 22:  
 23:   private IDictionary<string, string> Initialize(ApplicationSettingsContext dbContext)
 24:   {
 25:     foreach (var item in _initialSettings)
 26:     {
 27:       dbContext.Settings.Add(new ApplicationSetting(item.Key, item.Value));
 28:     }
 29:     return _initialSettings.ToDictionary(it => it.Key, it => it.Value, StringComparer.OrdinalIgnoreCase);
 30:   }
 31: }



五、扩展方法AddDatabase

实例演示中用来注册DbConfigurationSource的扩展方法AddDatabase具有如下的定义。该方法首先调用ConfigurationBuilder的Build方法创建出一个Configuration对象,并调用后者的扩展方法GetConnectionString根据指定的连接字符串名称得到完整的连接字符串。接下来我们调用构造函数创建一个DbConfigurationSource对象并注册到ConfigurationBuilder上。创建DbConfigurationSource对象指定的Action<DbContextOptionsBuilder>会完成针对连接字符串的设置。

  1: public static class DbConfigurationExtensions
  2: {
  3:   public static IConfigurationBuilder AddDatabase(this IConfigurationBuilder builder, string connectionStringName, IDictionary<string, string> initialSettings = null)
  4:   {
  5:     string connectionString = builder.Build().GetConnectionString(connectionStringName);
  6:     DbConfigurationSource source = new DbConfigurationSource(optionsBuilder => optionsBuilder.UseSqlServer(connectionString), initialSettings);
  7:     builder.Add(source);
  8:     return builder;
  9:   }
 10: }