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()方法里主要调用了应用程序初始化的逻辑。
this.Logger = this.CreateLogger();
this.ModuleCatalog = this.CreateModuleCatalog();
this.ConfigureModuleCatalog();
this.Container = this.CreateContainer();
this.ConfigureContainer();
protected override void ConfigureServiceLocator() { ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>()); }
this.ConfigureRegionAdapterMappings();
this.ConfigureDefaultRegionBehaviors();
protected override void RegisterFrameworkExceptionTypes() { base.RegisterFrameworkExceptionTypes(); ExceptionExtensions.RegisterFrameworkExceptionType( typeof(Microsoft.Practices.Unity.ResolutionFailedException)); }
this.Shell = this.CreateShell();
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(); }
if (this.Container.IsRegistered<IModuleManager>()) { this.Logger.Log(Resources.InitializingModules, Category.Debug, Priority.Low); this.InitializeModules(); }
至此,Run()方法的工作完成。
3. 配置DI Container
UnityBootstrapper另一个重要的方法就是ConfigureContainer()。这个方法主要功能
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); } }