PageRenderTime 32ms CodeModel.GetById 23ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism.MefExtensions/Modularity/MefModuleManager.cs

#
C# | 127 lines | 62 code | 9 blank | 56 comment | 7 complexity | dcaf52357d53f702867bfaa35bdf2052 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.ComponentModel.Composition;
 20using System.Linq;
 21using Microsoft.Practices.Prism.Logging;
 22using Microsoft.Practices.Prism.Modularity;
 23
 24namespace Microsoft.Practices.Prism.MefExtensions.Modularity
 25{
 26    /// <summary>    
 27    /// Component responsible for coordinating the modules' type loading and module initialization process. 
 28    /// </summary>
 29    /// <remarks>
 30    /// This allows the MefBootstrapper to provide this class as a default implementation.
 31    /// If another implementation is found, this export will not be used.
 32    /// </remarks>
 33    [Export(typeof(IModuleManager))]
 34    public partial class MefModuleManager : ModuleManager, IPartImportsSatisfiedNotification
 35    {
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="MefModuleManager"/> class.
 38        /// </summary>
 39        /// <param name="moduleInitializer">Service used for initialization of modules.</param>
 40        /// <param name="moduleCatalog">Catalog that enumerates the modules to be loaded and initialized.</param>
 41        /// <param name="loggerFacade">Logger used during the load and initialization of modules.</param>
 42        [ImportingConstructor()]
 43        public MefModuleManager(
 44            IModuleInitializer moduleInitializer,
 45            IModuleCatalog moduleCatalog,
 46            ILoggerFacade loggerFacade)
 47            : base(moduleInitializer, moduleCatalog, loggerFacade)
 48        {
 49        }
 50
 51#if SILVERLIGHT
 52    /// <summary>
 53    /// Gets or sets the modules to be imported.
 54    /// </summary>
 55    /// <remarks>Import the available modules from the MEF container.
 56    /// Due to Silverlight/MEF restrictions this must be public.
 57    /// </remarks>
 58        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures"), ImportMany(AllowRecomposition = true)]
 59        public IEnumerable<Lazy<IModule, IModuleExport>> ImportedModules { get; set; }
 60#else
 61        /// <summary>
 62        /// Gets or sets the modules to be imported.
 63        /// </summary>
 64        /// <remarks>Import the available modules from the MEF container</remarks>
 65        [ImportMany(AllowRecomposition = true)]
 66        protected IEnumerable<Lazy<IModule, IModuleExport>> ImportedModules { get; set; }
 67#endif
 68
 69        /// <summary>
 70        /// Called when a part's imports have been satisfied and it is safe to use.
 71        /// </summary>
 72        /// <remarks>
 73        /// Whenever the MEF container loads new types that cause ImportedModules to be recomposed, this is called.
 74        /// This method ensures that as the MEF container discovered new modules, the ModuleCatalog is updated.
 75        /// </remarks>
 76        public virtual void OnImportsSatisfied()
 77        {
 78            // To prevent a double foreach loop, we key on the module type for anything already in the catalog.
 79            IDictionary<string, ModuleInfo> registeredModules = this.ModuleCatalog.Modules.ToDictionary(m => m.ModuleName);
 80
 81            foreach (Lazy<IModule, IModuleExport> lazyModule in this.ImportedModules)
 82            {
 83                // It is important that the Metadata.ModuleType is used here. 
 84                // Using GetType().Name would cause the Module to be constructed here rather than lazily when the module is needed.
 85                Type moduleType = lazyModule.Metadata.ModuleType;
 86
 87                ModuleInfo registeredModule = null;
 88                if (!registeredModules.TryGetValue(lazyModule.Metadata.ModuleName, out registeredModule))
 89                {
 90                    // If the module is not already in the catalog is it added.
 91                    ModuleInfo moduleInfo = new ModuleInfo()
 92                    {
 93                        ModuleName = lazyModule.Metadata.ModuleName,
 94                        ModuleType = moduleType.AssemblyQualifiedName,
 95                        InitializationMode = lazyModule.Metadata.InitializationMode,
 96                        State = lazyModule.Metadata.InitializationMode == InitializationMode.OnDemand ? ModuleState.NotStarted : ModuleState.ReadyForInitialization,
 97                    };
 98
 99                    if (lazyModule.Metadata.DependsOnModuleNames != null)
100                    {
101                        moduleInfo.DependsOn.AddRange(lazyModule.Metadata.DependsOnModuleNames);
102                    }
103
104                    this.ModuleCatalog.AddModule(moduleInfo);
105                }
106                else
107                {
108                    // If the module is already in the catalog then override the module type name from the imported module
109                    registeredModule.ModuleType = moduleType.AssemblyQualifiedName;
110                }
111            }
112
113            this.LoadModulesThatAreReadyForLoad();
114        }
115
116        /// <summary>
117        /// Checks if the module needs to be retrieved before it's initialized.
118        /// </summary>
119        /// <param name="moduleInfo">Module that is being checked if needs retrieval.</param>
120        /// <returns>True if the module needs to be retrieved.  Otherwise, false.</returns>
121        protected override bool ModuleNeedsRetrieval(ModuleInfo moduleInfo)
122        {
123            return this.ImportedModules == null
124                || !this.ImportedModules.Any(lazyModule => lazyModule.Metadata.ModuleName == moduleInfo.ModuleName);
125        }
126    }
127}