标签:oid uid created ensure null for red oba length
我们在前面对配置模型中默认提供的各种IConfigurationSource实现类型进行了深入详尽的介绍,如果它们依然不能满足项目中的需求,我们还可以通过自定义IConfigurationSource实现类型来支持我们希望的配置源。就配置数据的持久化方式来说,将配置存储在数据库中应该是一种常见的方式。接下来我们会创建一个针对数据库的IConfigurationSource实现类型,它采用Entity Framework Core来完成数据库的存取操作。我们将这个自定义ConfigurationSource命名为DbConfigurationSource。在正式介绍它的实现之前,我们先来看看它在项目中的应用。我们将配置保存在SQL Server数据库中的某个数据表中,并采用Entity Framework Core来读取它。我们将连接字符串作为配置定义在一个名为“appSettings.json”的JSON文件中。
{
"connectionStrings": {
"DefaultDb": "Server = ... ; Database=...; Uid = ...; Pwd = ..."
}
}
在如下所示的演示程序中,我们首先创建了一个ConfigurationBuilder对象,并在它上面注册了一个指向connectionString.json文件的JsonConfigurationSource对象。针对DbConfigurationSource对象的注册体现在扩展方法AddDatabase上,这个方法具有两个参数,分别代表连接字符串的名称和初始的配置数据。前者正是connectionString.json设置的连接字符串名称DefaultDb,后者是一个字典对象,它提供的原始配置正好可以构成一个Profile对象。在利用ConfigurationBuilde对象创建出相应的IConfiguration对象之后,我们读取配置将其绑定为一个Profile对象。
public class Program
{
static void Main()
{
var initialSettings = new Dictionary<string, string>
{
["Gender"] = "Male",
["Age"] = "18",
["ContactInfo:EmailAddress"] = "foobar@outlook.com",
["ContactInfo:PhoneNo"] = "123456789"
};
var profile = new ConfigurationBuilder()
.AddJsonFile("appSettings.json")
.AddDatabase("DefaultDb", initialSettings)
.Build()
.Get<Profile>();
Debug.Assert(profile.Gender == Gender.Male);
Debug.Assert(profile.Age == 18);
Debug.Assert(profile.ContactInfo.EmailAddress == "foobar@outlook.com");
Debug.Assert(profile.ContactInfo.PhoneNo == "123456789");
}
}
如上面的代码片断所示,针对DbConfigurationSource的应用仅仅体现在我们为IConfigurationBuilder对象定义的AddDatabase扩展方法上,所以使用起来是非常方便的,那么这个扩展方法背后有着怎样的逻辑实现呢?DbConfigurationSource采用Entity Framework Core并以Code First的方式进行数据操作,如下所示的ApplicationSetting是表示基本配置项的POCO类型,我们将配置项的Key以小写的方式存储。另一个ApplicationSettingsContext是对应的DbContext类型。
[Table("ApplicationSettings")]
public class ApplicationSetting
{
private string key;
[Key]
public string Key
{
get { return key; }
set { key = value.ToLowerInvariant(); }
}
[Required]
[MaxLength(512)]
public string Value { get; set; }
public ApplicationSetting()
{ }
public ApplicationSetting(string key, string value)
{
Key = key;
Value = value;
}
}
public class ApplicationSettingsContext : DbContext
{
public ApplicationSettingsContext(DbContextOptions options) : base(options)
{ }
public DbSet<ApplicationSetting> Settings { get; set; }
}
如下所示的是DbConfigurationSource类型的定义,它的构造函数具有两个参数,第一个参数类型为Action<DbContextOptionsBuilder>,我们用这个委托对象来对创建DbContext采用的DbContextOptions进行设置,另一个可选的参数用来指定一些需要自动初始化的配置项。DbConfigurationSource在重写的Build方法中利用这两个对象 创建一个DbConfigurationProvider对象。
public class DbConfigurationSource : IConfigurationSource
{
private Action<DbContextOptionsBuilder> _setup;
private IDictionary<string, string> _initialSettings;
public DbConfigurationSource(Action<DbContextOptionsBuilder> setup, IDictionary<string, string> initialSettings = null)
{
_setup = setup;
_initialSettings = initialSettings;
}
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new DbConfigurationProvider(_setup, _initialSettings);
}
}
DbConfigurationProvider派生于抽象类ConfigurationProvider。在重写的Load方法中,它会根据提供的Action<DbContextOptionsBuilder>创建ApplicationSettingsContext对象,并利用它从数据库中读取配置数据并转换成字典对象并赋值给代表配置字典的Data属性 。如果数据表中没有数据,该方法还会利用这个DbContext对象将提供的初始化配置添加到数据库中。
public class DbConfigurationProvider: ConfigurationProvider
{
private readonly IDictionary<string, string> _initialSettings;
private readonly Action<DbContextOptionsBuilder> _setup;
public DbConfigurationProvider(Action<DbContextOptionsBuilder> setup, IDictionary<string, string> initialSettings)
{
_setup = setup;
_initialSettings = initialSettings?? new Dictionary<string, string>() ;
}
public override void Load()
{
var builder = new DbContextOptionsBuilder<ApplicationSettingsContext>();
_setup(builder);
using (ApplicationSettingsContext dbContext = new ApplicationSettingsContext(builder.Options))
{
dbContext.Database.EnsureCreated();
Data = dbContext.Settings.Any()
? dbContext.Settings.ToDictionary(it => it.Key, it => it.Value, StringComparer.OrdinalIgnoreCase)
: Initialize(dbContext);
}
}
private IDictionary<string, string> Initialize( ApplicationSettingsContext dbContext)
{
foreach (var item in _initialSettings)
{
dbContext.Settings.Add(new ApplicationSetting(item.Key, item.Value));
}
return _initialSettings.ToDictionary(it => it.Key, it => it.Value, StringComparer.OrdinalIgnoreCase);
}
}
实例演示中用来注册DbConfigurationSource对象的扩展方法AddDatabase具有如下的定义。该方法首先调用IConfigurationBuilder对象的Build方法创建出一个IConfiguration对象,并调用该对象[A5] 的扩展方法GetConnectionString根据指定的连接字符串名称得到完整的连接字符串。接下来我们调用构造函数创建一个DbConfigurationSource对象并注册到ConfigurationBuilder对象上。创建DbConfigurationSource对象时指定的Action<DbContextOptionsBuilder>会完成针对连接字符串的设置。
public static class DbConfigurationExtensions
{
public static IConfigurationBuilder AddDatabase( this IConfigurationBuilder builder, string connectionStringName, IDictionary<string, string> initialSettings = null)
{
var connectionString = builder.Build() .GetConnectionString(connectionStringName);
var source = new DbConfigurationSource( optionsBuilder => optionsBuilder.UseSqlServer(connectionString), initialSettings);
builder.Add(source);
return builder;
}
}
[ASP.NET Core 3框架揭秘] 配置[9]:自定义配置源
标签:oid uid created ensure null for red oba length
原文地址:https://blog.51cto.com/14650697/2461168