Asp.net 了解如何从数据库加载Nop商务设置
我正在与Nop Commerce合作,不知是否有人能帮我解决我的困惑 我对代码进行了多次调试,试图找出web应用程序启动时如何加载设置。我就是不明白 所有设置类都实现Asp.net 了解如何从数据库加载Nop商务设置,asp.net,asp.net-mvc,asp.net-mvc-3,autofac,nopcommerce,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Autofac,Nopcommerce,我正在与Nop Commerce合作,不知是否有人能帮我解决我的困惑 我对代码进行了多次调试,试图找出web应用程序启动时如何加载设置。我就是不明白 所有设置类都实现ISettings接口。让我们以客户设置为例。。我发现它由CustomerSettings类表示。数据库中有一个设置表。客户设置的数据如下所示: customersettings.usernamesenabled customersettings.checkusernameavailabilityenabled customerse
ISettings
接口。让我们以客户设置为例。。我发现它由CustomerSettings
类表示。数据库中有一个设置表
。客户设置的数据如下所示:
customersettings.usernamesenabled
customersettings.checkusernameavailabilityenabled
customersettings.allowuserstochangeusernames
... and so on...
如何将这些设置从customersettings
映射到customersettings
类,以及将usernamesenabled
之类的属性映射到customersettings类中的usernamesenabled
属性?为什么要这样实施呢
我知道它与DependencyRegister
类中的以下代码有关:
builder.RegisterGeneric(typeof(ConfigurationProvider<>)).As(typeof(IConfigurationProvider<>));
builder.RegisterSource(new SettingsSource());
builder.RegisterGeneric(typeof(ConfigurationProvider)).As(typeof(IConfigurationProvider));
RegisterSource(新设置source());
如果有人能给我指出正确的方向,我将不胜感激。希望我没有迟到
要了解该结构是如何创建的,只需了解几个相关点:
-Nop.Services.Configuration.ConfigurationProvider class
-Nop.Services.Configuration.ISettingsService interface
-Nop.Services.Configuration.SettingsService class
SettingsService只提供从存储库存储和检索设置的功能,并实现一些缓存功能
ConfigurationProvider完成了真正的魔术
让我们看看BuildConfiguration
方法:
// get properties we can write to
var properties = from prop in typeof(TSettings).GetProperties()
where prop.CanWrite && prop.CanRead
let setting = _settingService.GetSettingByKey<string>(typeof(TSettings).Name + "." + prop.Name)
where setting != null
where CommonHelper.GetNopCustomTypeConverter(prop.PropertyType).CanConvertFrom(typeof(string))
where CommonHelper.GetNopCustomTypeConverter(prop.PropertyType).IsValid(setting)
let value = CommonHelper.GetNopCustomTypeConverter(prop.PropertyType).ConvertFromInvariantString(setting)
select new { prop, value };
//获取我们可以写入的属性
var properties=来自typeof(TSettings.GetProperties()中的prop
其中prop.CanWrite和&prop.CanRead
let setting=\u settingService.GetSettingByKey(typeof(TSettings.Name+“+prop.Name)
设置在哪里!=无效的
其中CommonHelper.GetNopCustomTypeConverter(prop.PropertyType).CanConvertFrom(typeof(string))
其中CommonHelper.GetNopCustomTypeConverter(prop.PropertyType).IsValid(设置)
让value=CommonHelper.GetNopCustomTypeConverter(prop.PropertyType).ConvertFromInvariantString(设置)
选择新{prop,value};
使用反射,将检查*Settings类(例如CustomerSettings),并使用其属性从服务加载相应的设置
然后,它将转换回存储为字符串的值(您可以检查NopCustomTypeConverter以查看序列化是如何发生的),并将其分配回设置实体:
properties.ToList().ForEach(p=>p.prop.SetValue(Settings,p.value,null))代码>
另一种方法SaveSettings(TSettings settings)的作用正好相反,它采用设置实体并将其分解,以ClassName+PropertyValue的形式生成键-值对)
它是这样实现的,因为它部署了来自和其他模式的概念,以确保可维护性(基于API的组件化、可测试性ecc)