上一章说到,GetWidgetZones()方法返回了IWidgetPlugin接口, 那么,IWidgetPlugin这些插件如何注册的呢,这就要看下面了:
路径:\Presentation\Nop.Web\Global.asax.cs
protected void Application_Start() { //initialize engine context EngineContext.Initialize(false); ... EngineContext.Initialize(false);中注册了所有的插件,下面我们具体跟踪一下是怎么注册的:路径:\Libraries\Nop.Core\Infrastructure\EngineContext.cs
/// <summary> /// Initializes a static instance of the Nop factory. /// </summary> /// <param name="forceRecreate">Creates a new factory instance even though the factory has been previously initialized.</param> [MethodImpl(MethodImplOptions.Synchronized)] public static IEngine Initialize(bool forceRecreate) { if (Singleton<IEngine>.Instance == null || forceRecreate) { var config = ConfigurationManager.GetSection("NopConfig") as NopConfig; Singleton<IEngine>.Instance = CreateEngineInstance(config); Singleton<IEngine>.Instance.Initialize(config); } return Singleton<IEngine>.Instance; }CreateEngineInstance(NopConfig config)方法如下:
/// <summary> /// Creates a factory instance and adds a http application injecting facility. /// </summary> /// <param name="config">Config</param> /// <returns>New engine instance</returns> protected static IEngine CreateEngineInstance(NopConfig config) { if (config != null && !string.IsNullOrEmpty(config.EngineType)) { var engineType = Type.GetType(config.EngineType); if (engineType == null) throw new ConfigurationErrorsException("The type '" + config.EngineType + "' could not be found. Please check the configuration at /configuration/nop/engine[@engineType] or check for missing assemblies."); if (!typeof(IEngine).IsAssignableFrom(engineType)) throw new ConfigurationErrorsException("The type '" + engineType + "' doesn't implement 'Nop.Core.Infrastructure.IEngine' and cannot be configured in /configuration/nop/engine[@engineType] for that purpose."); return Activator.CreateInstance(engineType) as IEngine; } return new NopEngine(); }由于配置文件中Engine Type为空(<Engine Type="" />)所以,返回的实例是new NopEngine();所以Singleton<IEngine>.Instance.Initialize(config)调用的是:路径:\Libraries\Nop.Core\Infrastructure\NopEngine.cs
/// <summary> /// Initialize components and plugins in the nop environment. /// </summary> /// <param name="config">Config</param> public void Initialize(NopConfig config) { //register dependencies RegisterDependencies(config); //startup tasks if (!config.IgnoreStartupTasks) { RunStartupTasks(); } }然后我们看一下RegisterDependencies(config)这个方法: /// <summary> /// Register dependencies /// </summary> /// <param name="config">Config</param> protected virtual void RegisterDependencies(NopConfig config) { var builder = new ContainerBuilder(); var container = builder.Build(); //we create new instance of ContainerBuilder //because Build() or Update() method can only be called once on a ContainerBuilder. //dependencies var typeFinder = new WebAppTypeFinder(config); builder = new ContainerBuilder(); builder.RegisterInstance(config).As<NopConfig>().SingleInstance(); builder.RegisterInstance(this).As<IEngine>().SingleInstance(); builder.RegisterInstance(typeFinder).As<ITypeFinder>().SingleInstance(); builder.Update(container); //register dependencies provided by other assemblies builder = new ContainerBuilder(); var drTypes = typeFinder.FindClassesOfType<IDependencyRegistrar>(); var drInstances = new List<IDependencyRegistrar>(); foreach (var drType in drTypes) drInstances.Add((IDependencyRegistrar) Activator.CreateInstance(drType)); //sort drInstances = drInstances.AsQueryable().OrderBy(t => t.Order).ToList(); foreach (var dependencyRegistrar in drInstances) dependencyRegistrar.Register(builder, typeFinder); builder.Update(container); this._containerManager = new ContainerManager(container); //set dependency resolver DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); }这里使用autofac注册了IDependencyRegistrar类型,根据Order成员变量进行排序后,循环这些实现类并调用 void Register(ContainerBuilder builder, ITypeFinder typeFinder)方法来进行注册。我查找了一下所有实现接口的方法:
\nopCommerce\Presentation\Nop.Web\Infrastructure\DependencyRegistrar.cs(11): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Presentation\Nop.Web.Framework\DependencyRegistrar.cs(58): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Presentation\Nop.Web\Administration\Infrastructure\DependencyRegistrar.cs(10): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Plugins\Nop.Plugin.Feed.Froogle\DependencyRegistrar.cs(14): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Plugins\Nop.Plugin.Tax.CountryStateZip\DependencyRegistrar.cs(14): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Plugins\Nop.Plugin.Tax.CountryStateZip\Infrastructure\DependencyRegistrar.cs(9): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Plugins\Nop.Plugin.Shipping.ByWeight\DependencyRegistrar.cs(14): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Plugins\Nop.Plugin.ExternalAuth.Facebook\DependencyRegistrar.cs(8): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Plugins\Nop.Plugin.Widgets.NivoSlider\Infrastructure\DependencyRegistrar.cs(10): public class DependencyRegistrar : IDependencyRegistrar \nopCommerce\Plugins\Nop.Plugin.Misc.FacebookShop\Infrastructure\DependencyRegistrar.cs(10): public class DependencyRegistrar : IDependencyRegistrarOrder为0的是Framework层,它的注册方法如下:
路径:\nopCommerce\Presentation\Nop.Web.Framework\DependencyRegistrar.cs
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder) { //HTTP context and other related stuff builder.Register(c => //register FakeHttpContext when HttpContext is not available HttpContext.Current != null ? (new HttpContextWrapper(HttpContext.Current) as HttpContextBase) : (new FakeHttpContext("~/") as HttpContextBase)) .As<HttpContextBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve<HttpContextBase>().Request) .As<HttpRequestBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve<HttpContextBase>().Response) .As<HttpResponseBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve<HttpContextBase>().Server) .As<HttpServerUtilityBase>() .InstancePerLifetimeScope(); builder.Register(c => c.Resolve<HttpContextBase>().Session) .As<HttpSessionStateBase>() .InstancePerLifetimeScope(); //web helper builder.RegisterType<WebHelper>().As<IWebHelper>().InstancePerLifetimeScope(); //user agent helper builder.RegisterType<UserAgentHelper>().As<IUserAgentHelper>().InstancePerLifetimeScope(); //controllers builder.RegisterControllers(typeFinder.GetAssemblies().ToArray()); //data layer var dataSettingsManager = new DataSettingsManager(); var dataProviderSettings = dataSettingsManager.LoadSettings(); builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>(); builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency(); builder.Register(x => x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency(); if (dataProviderSettings != null && dataProviderSettings.IsValid()) { var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings()); var dataProvider = efDataProviderManager.LoadDataProvider(); dataProvider.InitConnectionFactory(); builder.Register<IDbContext>(c => new NopObjectContext(dataProviderSettings.DataConnectionString)).InstancePerLifetimeScope(); } else { builder.Register<IDbContext>(c => new NopObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerLifetimeScope(); } builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope(); //plugins builder.RegisterType<PluginFinder>().As<IPluginFinder>().InstancePerLifetimeScope(); builder.RegisterType<OfficialFeedManager>().As<IOfficialFeedManager>().InstancePerLifetimeScope(); //cache manager builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().Named<ICacheManager>("nop_cache_static").SingleInstance(); builder.RegisterType<PerRequestCacheManager>().As<ICacheManager>().Named<ICacheManager>("nop_cache_per_request").InstancePerLifetimeScope(); //work context builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerLifetimeScope(); //store context builder.RegisterType<WebStoreContext>().As<IStoreContext>().InstancePerLifetimeScope(); //services builder.RegisterType<BackInStockSubscriptionService>().As<IBackInStockSubscriptionService>().InstancePerLifetimeScope(); builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerLifetimeScope(); builder.RegisterType<CompareProductsService>().As<ICompareProductsService>().InstancePerLifetimeScope(); builder.RegisterType<RecentlyViewedProductsService>().As<IRecentlyViewedProductsService>().InstancePerLifetimeScope(); builder.RegisterType<ManufacturerService>().As<IManufacturerService>().InstancePerLifetimeScope(); builder.RegisterType<PriceFormatter>().As<IPriceFormatter>().InstancePerLifetimeScope(); builder.RegisterType<ProductAttributeFormatter>().As<IProductAttributeFormatter>().InstancePerLifetimeScope(); builder.RegisterType<ProductAttributeParser>().As<IProductAttributeParser>().InstancePerLifetimeScope(); builder.RegisterType<ProductAttributeService>().As<IProductAttributeService>().InstancePerLifetimeScope(); builder.RegisterType<ProductService>().As<IProductService>().InstancePerLifetimeScope(); builder.RegisterType<CopyProductService>().As<ICopyProductService>().InstancePerLifetimeScope(); builder.RegisterType<SpecificationAttributeService>().As<ISpecificationAttributeService>().InstancePerLifetimeScope(); builder.RegisterType<ProductTemplateService>().As<IProductTemplateService>().InstancePerLifetimeScope(); builder.RegisterType<CategoryTemplateService>().As<ICategoryTemplateService>().InstancePerLifetimeScope(); builder.RegisterType<ManufacturerTemplateService>().As<IManufacturerTemplateService>().InstancePerLifetimeScope(); builder.RegisterType<TopicTemplateService>().As<ITopicTemplateService>().InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<ProductTagService>().As<IProductTagService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType<AddressAttributeFormatter>().As<IAddressAttributeFormatter>().InstancePerLifetimeScope(); builder.RegisterType<AddressAttributeParser>().As<IAddressAttributeParser>().InstancePerLifetimeScope(); builder.RegisterType<AddressAttributeService>().As<IAddressAttributeService>().InstancePerLifetimeScope(); builder.RegisterType<AddressService>().As<IAddressService>().InstancePerLifetimeScope(); builder.RegisterType<AffiliateService>().As<IAffiliateService>().InstancePerLifetimeScope(); builder.RegisterType<VendorService>().As<IVendorService>().InstancePerLifetimeScope(); builder.RegisterType<SearchTermService>().As<ISearchTermService>().InstancePerLifetimeScope(); builder.RegisterType<GenericAttributeService>().As<IGenericAttributeService>().InstancePerLifetimeScope(); builder.RegisterType<FulltextService>().As<IFulltextService>().InstancePerLifetimeScope(); builder.RegisterType<MaintenanceService>().As<IMaintenanceService>().InstancePerLifetimeScope(); builder.RegisterType<CustomerAttributeParser>().As<ICustomerAttributeParser>().InstancePerLifetimeScope(); builder.RegisterType<CustomerAttributeService>().As<ICustomerAttributeService>().InstancePerLifetimeScope(); builder.RegisterType<CustomerService>().As<ICustomerService>().InstancePerLifetimeScope(); builder.RegisterType<CustomerRegistrationService>().As<ICustomerRegistrationService>().InstancePerLifetimeScope(); builder.RegisterType<CustomerReportService>().As<ICustomerReportService>().InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<PermissionService>().As<IPermissionService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<AclService>().As<IAclService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<PriceCalculationService>().As<IPriceCalculationService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType<GeoLookupService>().As<IGeoLookupService>().InstancePerLifetimeScope(); builder.RegisterType<CountryService>().As<ICountryService>().InstancePerLifetimeScope(); builder.RegisterType<CurrencyService>().As<ICurrencyService>().InstancePerLifetimeScope(); builder.RegisterType<MeasureService>().As<IMeasureService>().InstancePerLifetimeScope(); builder.RegisterType<StateProvinceService>().As<IStateProvinceService>().InstancePerLifetimeScope(); builder.RegisterType<StoreService>().As<IStoreService>().InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<StoreMappingService>().As<IStoreMappingService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType<DiscountService>().As<IDiscountService>().InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<SettingService>().As<ISettingService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); builder.RegisterSource(new SettingsSource()); //pass MemoryCacheManager as cacheManager (cache locales between requests) builder.RegisterType<LocalizationService>().As<ILocalizationService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache locales between requests) builder.RegisterType<LocalizedEntityService>().As<ILocalizedEntityService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType<LanguageService>().As<ILanguageService>().InstancePerLifetimeScope(); builder.RegisterType<DownloadService>().As<IDownloadService>().InstancePerLifetimeScope(); builder.RegisterType<PictureService>().As<IPictureService>().InstancePerLifetimeScope(); builder.RegisterType<MessageTemplateService>().As<IMessageTemplateService>().InstancePerLifetimeScope(); builder.RegisterType<QueuedEmailService>().As<IQueuedEmailService>().InstancePerLifetimeScope(); builder.RegisterType<NewsLetterSubscriptionService>().As<INewsLetterSubscriptionService>().InstancePerLifetimeScope(); builder.RegisterType<CampaignService>().As<ICampaignService>().InstancePerLifetimeScope(); builder.RegisterType<EmailAccountService>().As<IEmailAccountService>().InstancePerLifetimeScope(); builder.RegisterType<WorkflowMessageService>().As<IWorkflowMessageService>().InstancePerLifetimeScope(); builder.RegisterType<MessageTokenProvider>().As<IMessageTokenProvider>().InstancePerLifetimeScope(); builder.RegisterType<Tokenizer>().As<ITokenizer>().InstancePerLifetimeScope(); builder.RegisterType<EmailSender>().As<IEmailSender>().InstancePerLifetimeScope(); builder.RegisterType<CheckoutAttributeFormatter>().As<ICheckoutAttributeFormatter>().InstancePerLifetimeScope(); builder.RegisterType<CheckoutAttributeParser>().As<ICheckoutAttributeParser>().InstancePerLifetimeScope(); builder.RegisterType<CheckoutAttributeService>().As<ICheckoutAttributeService>().InstancePerLifetimeScope(); builder.RegisterType<GiftCardService>().As<IGiftCardService>().InstancePerLifetimeScope(); builder.RegisterType<OrderService>().As<IOrderService>().InstancePerLifetimeScope(); builder.RegisterType<OrderReportService>().As<IOrderReportService>().InstancePerLifetimeScope(); builder.RegisterType<OrderProcessingService>().As<IOrderProcessingService>().InstancePerLifetimeScope(); builder.RegisterType<OrderTotalCalculationService>().As<IOrderTotalCalculationService>().InstancePerLifetimeScope(); builder.RegisterType<ShoppingCartService>().As<IShoppingCartService>().InstancePerLifetimeScope(); builder.RegisterType<PaymentService>().As<IPaymentService>().InstancePerLifetimeScope(); builder.RegisterType<EncryptionService>().As<IEncryptionService>().InstancePerLifetimeScope(); builder.RegisterType<FormsAuthenticationService>().As<IAuthenticationService>().InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<UrlRecordService>().As<IUrlRecordService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); builder.RegisterType<ShipmentService>().As<IShipmentService>().InstancePerLifetimeScope(); builder.RegisterType<ShippingService>().As<IShippingService>().InstancePerLifetimeScope(); builder.RegisterType<TaxCategoryService>().As<ITaxCategoryService>().InstancePerLifetimeScope(); builder.RegisterType<TaxService>().As<ITaxService>().InstancePerLifetimeScope(); builder.RegisterType<TaxCategoryService>().As<ITaxCategoryService>().InstancePerLifetimeScope(); builder.RegisterType<DefaultLogger>().As<ILogger>().InstancePerLifetimeScope(); //pass MemoryCacheManager as cacheManager (cache settings between requests) builder.RegisterType<CustomerActivityService>().As<ICustomerActivityService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); bool databaseInstalled = DataSettingsHelper.DatabaseIsInstalled(); if (!databaseInstalled) { //installation service if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["UseFastInstallationService"]) && Convert.ToBoolean(ConfigurationManager.AppSettings["UseFastInstallationService"])) { builder.RegisterType<SqlFileInstallationService>().As<IInstallationService>().InstancePerLifetimeScope(); } else { builder.RegisterType<CodeFirstInstallationService>().As<IInstallationService>().InstancePerLifetimeScope(); } } builder.RegisterType<ForumService>().As<IForumService>().InstancePerLifetimeScope(); builder.RegisterType<PollService>().As<IPollService>().InstancePerLifetimeScope(); builder.RegisterType<BlogService>().As<IBlogService>().InstancePerLifetimeScope(); builder.RegisterType<WidgetService>().As<IWidgetService>().InstancePerLifetimeScope(); builder.RegisterType<TopicService>().As<ITopicService>().InstancePerLifetimeScope(); builder.RegisterType<NewsService>().As<INewsService>().InstancePerLifetimeScope(); builder.RegisterType<DateTimeHelper>().As<IDateTimeHelper>().InstancePerLifetimeScope(); builder.RegisterType<SitemapGenerator>().As<ISitemapGenerator>().InstancePerLifetimeScope(); builder.RegisterType<PageHeadBuilder>().As<IPageHeadBuilder>().InstancePerLifetimeScope(); builder.RegisterType<ScheduleTaskService>().As<IScheduleTaskService>().InstancePerLifetimeScope(); builder.RegisterType<ExportManager>().As<IExportManager>().InstancePerLifetimeScope(); builder.RegisterType<ImportManager>().As<IImportManager>().InstancePerLifetimeScope(); builder.RegisterType<PdfService>().As<IPdfService>().InstancePerLifetimeScope(); builder.RegisterType<ThemeProvider>().As<IThemeProvider>().InstancePerLifetimeScope(); builder.RegisterType<ThemeContext>().As<IThemeContext>().InstancePerLifetimeScope(); builder.RegisterType<ExternalAuthorizer>().As<IExternalAuthorizer>().InstancePerLifetimeScope(); builder.RegisterType<OpenAuthenticationService>().As<IOpenAuthenticationService>().InstancePerLifetimeScope(); builder.RegisterType<RoutePublisher>().As<IRoutePublisher>().SingleInstance(); //Register event consumers var consumers = typeFinder.FindClassesOfType(typeof(IConsumer<>)).ToList(); foreach (var consumer in consumers) { builder.RegisterType(consumer) .As(consumer.FindInterfaces((type, criteria) => { var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition()); return isMatch; }, typeof(IConsumer<>))) .InstancePerLifetimeScope(); } builder.RegisterType<EventPublisher>().As<IEventPublisher>().SingleInstance(); builder.RegisterType<SubscriptionService>().As<ISubscriptionService>().SingleInstance(); }插件相关如下:
//plugins builder.RegisterType<PluginFinder>().As<IPluginFinder>().InstancePerLifetimeScope(); builder.RegisterType<OfficialFeedManager>().As<IOfficialFeedManager>().InstancePerLifetimeScope();顺便关注下面代码,这些都是后面要用到的参数:
builder.RegisterType<WidgetService>().As<IWidgetService>().InstancePerLifetimeScope(); // 关注参数1 builder.RegisterType<StoreService>().As<IStoreService>().InstancePerLifetimeScope();ICacheManager注册很多,不一一列举下面再看一下基础设施层(Infrastructure)的注册方法:
路径:\Presentation\Nop.Web\Infrastructure\DependencyRegistrar.cs
public class DependencyRegistrar : IDependencyRegistrar { public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder) { //we cache presentation models between requests builder.RegisterType<BlogController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<CatalogController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<CountryController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<CommonController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<NewsController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<PollController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<ProductController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<ShoppingCartController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<TopicController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); builder.RegisterType<WidgetController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); //installation localization service builder.RegisterType<InstallationLocalizationService>().As<IInstallationLocalizationService>().InstancePerLifetimeScope(); } public int Order { get { return 2; } } } 这里注册了所有的业务层service,我们看到,最后一句WidgetController类型: builder.RegisterType<WidgetController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"));WidgetController其中有私有成员:IWidgetService _widgetService即为前面讲到的关注参数1,他的实例为:WidgetService。这样就和第二章对应起来了。 public partial class WidgetController : BasePublicController { #region Fields private readonly IWidgetService _widgetService; private readonly IStoreContext _storeContext; private readonly ICacheManager _cacheManager; #endregion #region Constructors public WidgetController(IWidgetService widgetService, IStoreContext storeContext, ICacheManager cacheManager) { this._widgetService = widgetService; this._storeContext = storeContext; this._cacheManager = cacheManager; } ...最后再看一下插件的注册方法:
路径:\Plugins\Nop.Plugin.Widgets.NivoSlider\Infrastructure\DependencyRegistrar.cs
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder) { //we cache presentation models between requests builder.RegisterType<WidgetsNivoSliderController>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")); } public int Order { get { return 2; } }
