《Prism 5.0源碼走讀》UnityBootstrapper

來源:互聯網
上載者:User

標籤:des   style   blog   color   使用   os   io   strong   

UnityBootstrapper (abstract class)繼承自Bootstrapper(abstract)類, 在Prism.UnityExtensions.Desktop project中。主要是為了支援Unity Container(Dependency Injection Container)。

開啟UnityBoostrapper原始碼我們可以看到這裡面主要有以下邏輯:

1. 定義Unity Container屬性

public IUnityContainer Container { get; protected set; }

2. 重寫Run()方法

 public override void Run(bool runWithDefaultConfiguration){         ......  }

這是Bootstrapper的入口方法,亦是整個應用的入口方法。在Run()方法裡主要調用了應用程式初始化的邏輯。

  • 建立Logger執行個體 (Bootstrapper實現,virtual 方法)
    this.Logger = this.CreateLogger();
  • 建立ModuleCatalog執行個體(Bootstrapper實現,virtual 方法)
    this.ModuleCatalog = this.CreateModuleCatalog();
  • 配置ModuleCatalog執行個體(Bootstrapper實現,virtual 方法)
    this.ConfigureModuleCatalog();
  • 建立DI Container執行個體 (UnityBootstrapper實現,virtual方法)
    this.Container = this.CreateContainer();
  • 配置DI Container (UnityBootstrapper實現,virtual方法)
    this.ConfigureContainer();
  • 配置ServiceLocator(UnityBootstrapper重寫Bootstrapper abstract方法)
    protected override void ConfigureServiceLocator(){      ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>());}
  • 配置Region Adapter的Mapping,主要有三種Region Adapter的mapping:Selector,ItemsControl,ContentControl (Bootstrapper實現,virtual方法)
    this.ConfigureRegionAdapterMappings();
  • 配置預設的Region Behaviors(Bootstrapper實現,virtaul方法。)主要有: BindRegionContextToDependencyObjectBehavior,RegionActiveAwareBehavior,SyncRegionContextWithHostBehavior等,具體可參考Boostrapper裡ConfigureDefaultRegionBehaviors方法。
    this.ConfigureDefaultRegionBehaviors();
  • 註冊Prism framework throw出來的Exception 類型(重寫Boostrapper方法)
    protected override void RegisterFrameworkExceptionTypes(){     base.RegisterFrameworkExceptionTypes();     ExceptionExtensions.RegisterFrameworkExceptionType(     typeof(Microsoft.Practices.Unity.ResolutionFailedException));}
  • 建立應用程式Shell(調用Boostrapper定義的abstract方法),在使用Prism時,需要實現此方法。
    this.Shell = this.CreateShell();
  • 如果建立Shell成功,則調用RegionManager裡的SetRegionManager和UpdateRegions方法來處理Region的關聯。後面再調用父類的初始化Shell方法(virtual,沒有具體實現)。
    if (this.Shell != null){    this.Logger.Log(Resources.SettingTheRegionManager, Category.Debug, Priority.Low);    RegionManager.SetRegionManager(this.Shell, this.Container.Resolve<IRegionManager>());    this.Logger.Log(Resources.UpdatingRegions, Category.Debug, Priority.Low);    RegionManager.UpdateRegions();    this.Logger.Log(Resources.InitializingShell, Category.Debug, Priority.Low);    this.InitializeShell();}
  • 初始化Modules,重寫父類方法
if (this.Container.IsRegistered<IModuleManager>()){    this.Logger.Log(Resources.InitializingModules, Category.Debug, Priority.Low);    this.InitializeModules();}

 

至此,Run()方法的工作完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.