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





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM