Prism初研究之Bootstrapper

Prism初研究之初始化应用

Bootstrapper

Bootstrapper类的责任是使用Prism类库初始化应用程序,抽象类Bootstrapper提供的函数大多都是虚方法。
Bootstrapper初始化过程

DI

UnityBootstrapper和MefBootstrapper类实现了大多数必须的功能。

Shell

在Prism应用中,创建Shell或者主窗口的责任是Bootstrapper的。因为Shell依赖的一些服务比如Region Manager需要在Shell显示之前初始化。

关键抉择

  • 是否使用Unity、MEF或者其它的dependency injection container,这决定Bootstrapper类创建;
  • 应用使用那些服务,这些服务需要注册到container中;
  • 决定创建日志服务是否需要,或者是否需要其它日志服务;
  • 决定哪些模块是希望应用发现的,通过显示代码声明、文件夹扫描、配置文件还是XAML方式配置;

核心步骤

创建Bootstrapper

实现CreateShell方法

返回你应用程序Shell类的实例,可以根据需求选择创建Shell对象或者从Container中获取Shell。

 
 
  1. protected override DependencyObject CreateShell()
  2. {
  3. return this.Container.Resolve<Shell>();
  4. }
实现InitializeShell方法

创建Shell之后显示之前,需要为应用程序设置主界面。

 
 
  1. protected override void InitializeShell()
  2. {
  3. base.InitializeShell();
  4. //基类的InitializeShell()没有做任何事。
  5. App.Current.MainWindow = (Window) this.Shell;
  6. App.Current.MainWindow.Show();
  7. }

创建并配置Module Catalog

抽象类Bootstrapper的CreateModuleCatalog()返回new ModuleCatalog。

 
 
  1. protected virtual IModuleCatalog CreateModuleCatalog()
  2. {
  3. return new ModuleCatalog();
  4. }

UnityBootstrapper和MEFBootstrapper类的Run方法都调用CreateModuleCatalog()方法,为ModuleCatalog属性设置返回值。

创建并配置Container

Container在Prism应用中扮演关键角色,Prism类库和应用程序需要Container来提供服务、模块等的依赖关系。在Container的Configuration过程中,需要的核心服务被注册。

核心服务(与应用无关)
服务接口描述
IModuleManager检索、初始化应用程序模块的接口
IModuleCatalog包含模块的元数据;Prism框架提供一些不同的模块分类
IModuleInitializer初始化模块
IRegionManager注册、检索Region的接口
IEventAggregator一组松耦合的事件
ILoggerFacade框架为日志服务提供了包装机制,日志服务在bootstrapper.Run()方法中注册。使用CreateLogger方法的返回值,所以如果需要定制日志服务,需要覆写CreateLogger()方法
IServiceLocator允许Prism 类库访问Container。如果需要定制或者扩展类库将会很有用
与应用相关的服务(Stock Trader RI)
Stock Trader RI 中的服务描述
IMarketFeedService提供实时的市场数据,PositionSummaryViewModel使用此服务
IMarketHistoryService提供市场历史数据
IAccountPositionService提供基金列表
IOrdersService提交订单服务
INewsFeedService提供选择基金的新闻
IWatchListService控制添加新的监控数据到监控列表
在UnityBootstrapper中创建并配置Container
CreateContainer

UnityBootstrapper的CreateContainer方法返回一个UnityContainer的实例,通常这种行为不需要被改变,如果要改变,覆盖这个方法即可。

ConfigureContainer

ConfigureContainer方法注册了一系列Prism核心服务(默认)

 
 
  1. // UnityBootstrapper.cs
  2. protected virtual void ConfigureContainer()
  3. {
  4. ...
  5. if (useDefaultConfiguration)
  6. {
  7. RegisterTypeIfMissing(typeof(IServiceLocator), typeof(UnityServiceLocatorAdapter), true);
  8. RegisterTypeIfMissing(typeof(IModuleInitializer), typeof(ModuleInitializer), true);
  9. RegisterTypeIfMissing(typeof(IModuleManager), typeof(ModuleManager), true);
  10. RegisterTypeIfMissing(typeof(RegionAdapterMappings), typeof(RegionAdapterMappings), true);
  11. RegisterTypeIfMissing(typeof(IRegionManager), typeof(RegionManager), true);
  12. RegisterTypeIfMissing(typeof(IEventAggregator), typeof(EventAggregator), true);
  13. RegisterTypeIfMissing(typeof(IRegionViewRegistry), typeof(RegionViewRegistry), true);
  14. RegisterTypeIfMissing(typeof(IRegionBehaviorFactory), typeof(RegionBehaviorFactory), true);
  15. RegisterTypeIfMissing(typeof(IRegionNavigationJournalEntry), typeof(RegionNavigationJournalEntry), false);
  16. RegisterTypeIfMissing(typeof(IRegionNavigationJournal), typeof(RegionNavigationJournal), false);
  17. RegisterTypeIfMissing(typeof(IRegionNavigationService), typeof(RegionNavigationService), false);
  18. RegisterTypeIfMissing(typeof(IRegionNavigationContentLoader), typeof(UnityRegionNavigationContentLoader), true);
  19. }
  20. }

RegisterTypeIfMissing()方法确保服务不会被注册两次。
如果不想默认注册服务,调用Bootstrapper.Run(false),然后手动注册服务。

示例:QuickStartBootstrapper(Modularity QuickStart)
 
 
  1. protect override void ConfigureContainer()
  2. {
  3. base.ConfigureContainer();
  4. this.RegisterTypeIfMissing(typeof(IModuleTracker), typeof(ModuleTracker), true);
  5. this.Container.RegisterInstance<CallbackLogger>(this.callbackLogger);//CallbackLogger为单例
  6. }
在MefBootstrapper中创建并配置Container
CreateContainer

在CreateContainer中做了以下几件事:

  1. 创建一个AssemblyCatalog和一个CatalogExportProvider(CompositionContainer构造函数中);
  2. 返回一个CompositionContainer新实例。
ConfigureContainer

这个函数默认注册一组Prism核心服务:

 
 
  1. protected virtual void ConfigureContainer()
  2. {
  3. this.RegisterBootstrapperProvidedTypes();
  4. }
  5. protected virtual void RegisterBootstrapperProvidedTypes()
  6. {
  7. this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger);
  8. this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
  9. this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
  10. this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
  11. }

注意:在MefBootstrapper中,核心的服务都是单例。

示例:QuickStartBootstrapper()

MefBootstrapper另外两个可以重载的函数来创建和配置AggregateCatalog:

  • CreateAggregateCatalog;
  • ConfigureAggregateCatalog;
 
 
  1. protected override void ConfigureAggregateCatalog()
  2. {
  3. base.ConfigureAggregateCatalog();
  4. // Add this assembly to export ModuleTracker
  5. this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(QuickStartBootstrapper).Assembly));
  6. // Module A is referenced in in the project and directly in code.
  7. this.AggregateCatalog.Catalogs.Add(
  8. new AssemblyCatalog(typeof(ModuleA.ModuleA).Assembly));
  9. this.AggregateCatalog.Catalogs.Add(
  10. new AssemblyCatalog(typeof(ModuleC.ModuleC).Assembly));
  11. // Module B and Module D are copied to a directory as part of a post-build step.
  12. // These modules are not referenced in the project and are discovered by
  13. inspecting a directory.
  14. // Both projects have a post-build step to copy themselves into that directory.
  15. DirectoryCatalog catalog = new DirectoryCatalog("DirectoryModules");
  16. this.AggregateCatalog.Catalogs.Add(catalog);
  17. }




转载于:https://www.cnblogs.com/qianzi067/p/5804837.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值