如何从 NopCommerce v3.5 的设置 table 加载记录

How to load records from Setting table of NopCommerce v3.5

我正在尝试使用 NopCommerce v3.5 并试图用 Ninject v3.2 替换 Autofac。我一直在搜索NopCommerce如何从设置table加载记录,目前还不清楚。如果有的话,我想使用 Ninject 加载设置。感谢您的帮助。

这是 Nop.web.Framework.DependencyRegistrar.cs

中的示例代码
builder.RegisterType<SettingService>().As<ISettingService>()
    .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
    .InstancePerLifetimeScope();
builder.RegisterSource(new SettingsSource());

我会尽量解释清楚。

根据 Autofac's docsRegisterSource 允许您为您不知道或未考虑过的 class 注册工厂。它将类型与提供它们的规则绑定在一起。

这在此处如何应用?

当您从容器中需要 class 的实例时,例如 LoggerSettings,autofac 将检查您在 nopCommerce 拥有的不同依赖项注册商处声明的所有规则,并且它会询问报名来源:嘿嘿!我需要一个 LoggerSettings 的实例,你知道吗? RegistrationSource 检查它并说 "Yes, it implements ISettings, here you have the injection rule for it (in this case a lambda that retrieves the settings instance from a generic repository), keep it, next time you won't need to ask"。请注意,它将 return 类型的实例化规则,而不是实例本身。

作为替代方案,也许您可​​以抓取所有程序集,但对于具有此类插件的大型系统来说,这将是一件痛苦的事情。您也可以注册每个特定的 class,但在这种情况下这是不可行的。

要查看它的工作情况,请在 SettingsSource 的两个方法和委托内部设置几个断点并启动项目。

要使用 Ninject 执行此操作,您需要复制此行为。