问题
I'm trying to use NopCommerce v3.5 and trying to replace Autofac with Ninject v3.2. I've been searching how NopCommerce load records from Setting table, it's still not clear. And if ever, I want to load the settings using Ninject. Thank you for any help.
Here's a sample code in Nop.web.Framework.DependencyRegistrar.cs
builder.RegisterType<SettingService>().As<ISettingService>()
.WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"))
.InstancePerLifetimeScope();
builder.RegisterSource(new SettingsSource());
回答1:
I'll try to explain it as well as I can.
According to Autofac's docs, RegisterSource
allows you to register factories for classes that you don't know about or have considered. It will really late bind the types with the rules that provide them.
How does this apply here?
When you require an instance of a class such as LoggerSettings
from the container, autofac will check all the rules you have declared at the different dependency registrars that nopCommerce has, and it will ask the registration source: Hey! I need an instance of LoggerSettings, do you know something about this? The RegistrationSource checks it and says "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". Note that it will return the instantiation rule for the type, not the instance itself.
As an alternative, maybe you could do it crawling all assemblies, but for a large system with plugins like this, it would be a pain. You could also register each particular class, but in this case it is not doable.
To see it working, set a couple of breakpoints at the two methods of SettingsSource and inside the delegate and start the project.
To do it with Ninject you will need to replicate this behaviour.
来源:https://stackoverflow.com/questions/32944127/how-to-load-records-from-setting-table-of-nopcommerce-v3-5