【问题标题】:Understanding how Nop Commerce settings are loaded from the database了解如何从数据库加载 Nop Commerce 设置
【发布时间】:2012-05-19 16:43:03
【问题描述】:

我正在与Nop Commerce 合作,想知道是否有人可以帮助我解决我的困惑。

我已多次调试代码,试图找出在启动 Web 应用程序时如何加载设置。我就是不明白!

所有设置类都实现ISettings 接口。让我们以客户设置为例。我发现它由CustomerSettings 类表示。在数据库中有一个Setting table。客户设置数据如下所示:

customersettings.usernamesenabled
customersettings.checkusernameavailabilityenabled
customersettings.allowuserstochangeusernames
... and so on...

这些设置如何以及在何处从 customersettings 映射到 CustomerSettings 类以及像 usernamesenabled 这样的属性映射到 CustomerSettings 类中的 UsernamesEnabled 属性?为什么要以这种方式实施?

我知道这与DependencyRegistrar 类中的以下代码有关:

builder.RegisterGeneric(typeof(ConfigurationProvider<>)).As(typeof(IConfigurationProvider<>));
builder.RegisterSource(new SettingsSource());

如果有人能指出我正确的方向,那将不胜感激。

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 autofac nopcommerce


    【解决方案1】:

    希望我不会迟到。

    只需查看几个相关点即可了解该结构是如何创建的:

    -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 };
    

    使用反射检查 *Settings 类(例如 CustomerSettings),并使用它们的属性从服务加载相应的设置。

    然后它将存储为字符串的值转换回(您可以检查 NopCustomTypeConverter 以了解序列化是如何发生的)并将它们分配回设置实体:

    properties.ToList().ForEach(p =&gt; p.prop.SetValue(Settings, p.value, null));

    另一种方法,SaveSettings(TSettings settings) 则完全相反,采用 Setting 实体并将其分解,生成 ClassName+Propertyvalues 形式的键值对)

    之所以这样实现,是因为它部署了来自 IoCsegregation of interfacesn-tier 和其他模式的概念,以确保可维护性(基于 API 的组件化、可测试性 ecc)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      相关资源
      最近更新 更多