《Prism 5.0源码走读》UnityBootstrapper

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实现,返回一个UnityContainer的实例,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()方法的工作完成。

3. 配置DI Container

UnityBootstrapper另一个重要的方法就是ConfigureContainer()。这个方法主要功能

  1. 为Unity Container添加extension: UnityBootstrapperExtension.
  2. 把Prism框架里面主要的实例注入到Unity Container里面。
        protected virtual void ConfigureContainer()
        {
            this.Logger.Log(Resources.AddingUnityBootstrapperExtensionToContainer, Category.Debug, Priority.Low);
            this.Container.AddNewExtension<UnityBootstrapperExtension>();

            Container.RegisterInstance<ILoggerFacade>(Logger);

            this.Container.RegisterInstance(this.ModuleCatalog);

            if (useDefaultConfiguration)
            {
                RegisterTypeIfMissing(typeof(IServiceLocator), typeof(UnityServiceLocatorAdapter), true);
                RegisterTypeIfMissing(typeof(IModuleInitializer), typeof(ModuleInitializer), true);
                RegisterTypeIfMissing(typeof(IModuleManager), typeof(ModuleManager), true);
                RegisterTypeIfMissing(typeof(RegionAdapterMappings), typeof(RegionAdapterMappings), true);
                RegisterTypeIfMissing(typeof(IRegionManager), typeof(RegionManager), true);
                RegisterTypeIfMissing(typeof(IEventAggregator), typeof(EventAggregator), true);
                RegisterTypeIfMissing(typeof(IRegionViewRegistry), typeof(RegionViewRegistry), true);
                RegisterTypeIfMissing(typeof(IRegionBehaviorFactory), typeof(RegionBehaviorFactory), true);
                RegisterTypeIfMissing(typeof(IRegionNavigationJournalEntry), typeof(RegionNavigationJournalEntry), false);
                RegisterTypeIfMissing(typeof(IRegionNavigationJournal), typeof(RegionNavigationJournal), false);
                RegisterTypeIfMissing(typeof(IRegionNavigationService), typeof(RegionNavigationService), false);
                RegisterTypeIfMissing(typeof(IRegionNavigationContentLoader), typeof(UnityRegionNavigationContentLoader), true);
            }
        }
原文地址:https://www.cnblogs.com/codesee/p/3925539.html