Vous êtes sur la page 1sur 5

//============================================================================== ===== // Microsoft patterns & practices // Composite Application Guidance for Windows Presentation Foundation and Silver

light //============================================================================== ===== // Copyright (c) Microsoft Corporation. All rights reserved. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE. //============================================================================== ===== // The example companies, organizations, products, domain names, // e-mail addresses, logos, people, places, and events depicted // herein are fictitious. No association with any real company, // organization, product, domain name, email address, logo, person, // places, or events is intended or should be inferred. //============================================================================== ===== using System; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using Microsoft.Practices.Prism.Logging; using Microsoft.Practices.Prism.MefExtensions.Properties; using Microsoft.Practices.Prism.Modularity; using Microsoft.Practices.Prism.Regions; using Microsoft.Practices.ServiceLocation; using System.Collections.Generic; using System.Linq; namespace Microsoft.Practices.Prism.MefExtensions { /// <summary> /// Base class that provides a basic bootstrapping sequence that registers m ost of the Composite Application Library assets in a MEF <see cref="CompositionC ontainer"/>. /// </summary> /// <remarks> /// This class must be overriden to provide application specific configurati on. /// </remarks> public abstract class MefBootstrapper : Bootstrapper { /// <summary> /// Gets or sets the default <see cref="AggregateCatalog"/> for the appl ication. /// </summary> /// <value>The default <see cref="AggregateCatalog"/> instance.</value> protected AggregateCatalog AggregateCatalog { get; set; } /// <summary> /// Gets or sets the default <see cref="CompositionContainer"/> for the application. /// </summary> /// <value>The default <see cref="CompositionContainer"/> instance.</val ue> protected CompositionContainer Container { get; set; }

/// <summary> /// Run the bootstrapper process. /// </summary> /// <param name="runWithDefaultConfiguration">If <see langword="true"/>, registers default /// Composite Application Library services in the container. This is the default behavior.</param> public override void Run(bool runWithDefaultConfiguration) { this.Logger = this.CreateLogger(); if (this.Logger == null) { throw new InvalidOperationException(Resources.NullLoggerFacadeEx ception); } this.Logger.Log(Resources.LoggerWasCreatedSuccessfully, Category.Deb ug, Priority.Low); this.Logger.Log(Resources.CreatingModuleCatalog, Category.Debug, Pri ority.Low); this.ModuleCatalog = this.CreateModuleCatalog(); if (this.ModuleCatalog == null) { throw new InvalidOperationException(Resources.NullModuleCatalogE xception); } this.Logger.Log(Resources.ConfiguringModuleCatalog, Category.Debug, Priority.Low); this.ConfigureModuleCatalog(); this.Logger.Log(Resources.CreatingCatalogForMEF, Category.Debug, Pri ority.Low); this.AggregateCatalog = this.CreateAggregateCatalog(); this.Logger.Log(Resources.ConfiguringCatalogForMEF, Category.Debug, Priority.Low); this.ConfigureAggregateCatalog(); this.RegisterDefaultTypesIfMissing(); this.Logger.Log(Resources.CreatingMefContainer, Category.Debug, Prio rity.Low); this.Container = this.CreateContainer(); if (this.Container == null) { throw new InvalidOperationException(Resources.NullCompositionCon tainerException); } this.Logger.Log(Resources.ConfiguringMefContainer, Category.Debug, P riority.Low); this.ConfigureContainer(); this.Logger.Log(Resources.ConfiguringServiceLocatorSingleton, Catego ry.Debug, Priority.Low); this.ConfigureServiceLocator();

this.Logger.Log(Resources.ConfiguringRegionAdapters, Category.Debug, Priority.Low); this.ConfigureRegionAdapterMappings(); this.Logger.Log(Resources.ConfiguringDefaultRegionBehaviors, Categor y.Debug, Priority.Low); this.ConfigureDefaultRegionBehaviors(); this.Logger.Log(Resources.RegisteringFrameworkExceptionTypes, Catego ry.Debug, Priority.Low); this.RegisterFrameworkExceptionTypes(); this.Logger.Log(Resources.CreatingShell, Category.Debug, Priority.Lo w); this.Shell = this.CreateShell(); if (this.Shell != null) { this.Logger.Log(Resources.SettingTheRegionManager, Category.Debu g, Priority.Low); RegionManager.SetRegionManager(this.Shell, this.Container.GetExp ortedValue<IRegionManager>()); this.Logger.Log(Resources.UpdatingRegions, Category.Debug, Prior ity.Low); RegionManager.UpdateRegions(); this.Logger.Log(Resources.InitializingShell, Category.Debug, Pri ority.Low); this.InitializeShell(); } IEnumerable<Lazy<object, object>> exports = this.Container.GetExport s(typeof(IModuleManager), null, null); if ((exports != null) && (exports.Count() > 0)) { this.Logger.Log(Resources.InitializingModules, Category.Debug, P riority.Low); this.InitializeModules(); } this.Logger.Log(Resources.BootstrapperSequenceCompleted, Category.De bug, Priority.Low); } /// <summary> /// Configures the <see cref="AggregateCatalog"/> used by MEF. /// </summary> /// <remarks> /// The base implementation returns a new AggregateCatalog. /// </remarks> /// <returns>An <see cref="AggregateCatalog"/> to be used by the bootstr apper.</returns> protected virtual AggregateCatalog CreateAggregateCatalog() { return new AggregateCatalog(); } /// <summary> /// Configures the <see cref="AggregateCatalog"/> used by MEF. /// </summary>

/// <remarks> /// The base implementation does nothing. /// </remarks> protected virtual void ConfigureAggregateCatalog() { } /// <summary> /// Creates the <see cref="CompositionContainer"/> that will be used as the default container. /// </summary> /// <returns>A new instance of <see cref="CompositionContainer"/>.</retu rns> /// <remarks> /// The base implementation registers a default MEF catalog of exports o f key Prism types. /// Exporting your own types will replace these defaults. /// </remarks> [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability" , "CA2000:Dispose objects before losing scope", Justification = "The default export provider is in the container and disposed by MEF.")] protected virtual CompositionContainer CreateContainer() { CompositionContainer container = new CompositionContainer(this.Aggre gateCatalog); return container; } /// <summary> /// Configures the <see cref="CompositionContainer"/>. /// May be overwritten in a derived class to add specific type mappings required by the application. /// </summary> /// <remarks> /// The base implementation registers all the types direct instantiated by the bootstrapper with the container. /// If the method is overwritten, the new implementation should call the base class version. /// </remarks> protected virtual void ConfigureContainer() { this.RegisterBootstrapperProvidedTypes(); } /// <summary> /// Helper method for configuring the <see cref="CompositionContainer"/> . /// Registers defaults for all the types necessary for Prism to work, if they are not already registered. /// </summary> public virtual void RegisterDefaultTypesIfMissing() { this.AggregateCatalog = DefaultPrismServiceRegistrar.RegisterRequire dPrismServicesIfMissing(this.AggregateCatalog); } /// <summary> /// Helper method for configuring the <see cref="CompositionContainer"/>

. /// Registers all the types direct instantiated by the bootstrapper with the container. /// </summary> protected virtual void RegisterBootstrapperProvidedTypes() { this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger); this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatal og); this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceL ocatorAdapter(this.Container)); this.Container.ComposeExportedValue<AggregateCatalog>(this.Aggregate Catalog); } /// <summary> /// Configures the LocatorProvider for the <see cref="Microsoft.Practice s.ServiceLocation.ServiceLocator" />. /// </summary> /// <remarks> /// The base implementation also sets the ServiceLocator provider single ton. /// </remarks> protected override void ConfigureServiceLocator() { IServiceLocator serviceLocator = this.Container.GetExportedValue<ISe rviceLocator>(); ServiceLocator.SetLocatorProvider(() => serviceLocator); } /// /// /// /// /// er. /// </remarks> protected override void InitializeShell() { this.Container.ComposeParts(this.Shell); } /// <summary> /// Initializes the modules. May be overwritten in a derived class to us e a custom Modules Catalog /// </summary> protected override void InitializeModules() { IModuleManager manager = this.Container.GetExportedValue<IModuleMana ger>(); manager.Run(); } } } <summary> Initializes the shell. </summary> <remarks> The base implementation ensures the shell is composed in the contain

Vous aimerez peut-être aussi