Bre*_*ogt 6 asp.net asp.net-mvc autofac nopcommerce asp.net-mvc-3
我正在Nop Commerce和我一起工作并想知道是否有人可以帮助我解决我的困惑.
我已经多次调试代码,试图找出在启动Web应用程序时如何加载设置.我只是不明白!
所有设置类都实现了该ISettings接口.让我们以客户设置为例..我发现它是由CustomerSettings类代表的.在数据库中有一个Setting table.客户设置的数据看起来像这样:
customersettings.usernamesenabled
customersettings.checkusernameavailabilityenabled
customersettings.allowuserstochangeusernames
... and so on...
Run Code Online (Sandbox Code Playgroud)
每个这些设置如何以及在何处映射customersettings到CustomerSettings类以及usernamesenabled映射到UsernamesEnabledCustomerSettings类中的属性的属性?为什么这样实现?
我知道它与类中的以下代码有关DependencyRegistrar:
builder.RegisterGeneric(typeof(ConfigurationProvider<>)).As(typeof(IConfigurationProvider<>));
builder.RegisterSource(new SettingsSource());
Run Code Online (Sandbox Code Playgroud)
如果有人能指出我正确的方向,那么我们将不胜感激.
希望我不迟到.
要了解如何创建该结构,只需要了解几个相关点:
-Nop.Services.Configuration.ConfigurationProvider class
-Nop.Services.Configuration.ISettingsService interface
-Nop.Services.Configuration.SettingsService class
Run Code Online (Sandbox Code Playgroud)
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 };
Run Code Online (Sandbox Code Playgroud)
使用反射,*Settings类(例如,CustomerSettings)进行检查,并使用其属性从服务加载相应的设置.
然后它将存储为字符串的值转换回来(您可以检查NopCustomTypeConverter以查看序列化是如何发生的)并将它们分配回设置实体:
properties.ToList().ForEach(p => p.prop.SetValue(Settings, p.value, null));
另一种方法,SaveSettings(TSettings设置)完全相反,采用一个Setting实体并将其分解,以ClassName + Propertyvalues的形式生成键值对)
据实现像这样,因为它从部署概念的IoC,接口偏析,n层和其它图案,以确保可维护性(基于API组件化,可测试性ECC).
| 归档时间: |
|
| 查看次数: |
3328 次 |
| 最近记录: |