PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.ComponentModel.Composition;
  20. using System.Linq;
  21. using Microsoft.Practices.Prism.Logging;
  22. using Microsoft.Practices.Prism.Modularity;
  23. namespace Microsoft.Practices.Prism.MefExtensions.Modularity
  24. {
  25. /// <summary>
  26. /// Component responsible for coordinating the modules' type loading and module initialization process.
  27. /// </summary>
  28. /// <remarks>
  29. /// This allows the MefBootstrapper to provide this class as a default implementation.
  30. /// If another implementation is found, this export will not be used.
  31. /// </remarks>
  32. [Export(typeof(IModuleManager))]
  33. public partial class MefModuleManager : ModuleManager, IPartImportsSatisfiedNotification
  34. {
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="MefModuleManager"/> class.
  37. /// </summary>
  38. /// <param name="moduleInitializer">Service used for initialization of modules.</param>
  39. /// <param name="moduleCatalog">Catalog that enumerates the modules to be loaded and initialized.</param>
  40. /// <param name="loggerFacade">Logger used during the load and initialization of modules.</param>
  41. [ImportingConstructor()]
  42. public MefModuleManager(
  43. IModuleInitializer moduleInitializer,
  44. IModuleCatalog moduleCatalog,
  45. ILoggerFacade loggerFacade)
  46. : base(moduleInitializer, moduleCatalog, loggerFacade)
  47. {
  48. }
  49. #if SILVERLIGHT
  50. /// <summary>
  51. /// Gets or sets the modules to be imported.
  52. /// </summary>
  53. /// <remarks>Import the available modules from the MEF container.
  54. /// Due to Silverlight/MEF restrictions this must be public.
  55. /// </remarks>
  56. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures"), ImportMany(AllowRecomposition = true)]
  57. public IEnumerable<Lazy<IModule, IModuleExport>> ImportedModules { get; set; }
  58. #else
  59. /// <summary>
  60. /// Gets or sets the modules to be imported.
  61. /// </summary>
  62. /// <remarks>Import the available modules from the MEF container</remarks>
  63. [ImportMany(AllowRecomposition = true)]
  64. protected IEnumerable<Lazy<IModule, IModuleExport>> ImportedModules { get; set; }
  65. #endif
  66. /// <summary>
  67. /// Called when a part's imports have been satisfied and it is safe to use.
  68. /// </summary>
  69. /// <remarks>
  70. /// Whenever the MEF container loads new types that cause ImportedModules to be recomposed, this is called.
  71. /// This method ensures that as the MEF container discovered new modules, the ModuleCatalog is updated.
  72. /// </remarks>
  73. public virtual void OnImportsSatisfied()
  74. {
  75. // To prevent a double foreach loop, we key on the module type for anything already in the catalog.
  76. IDictionary<string, ModuleInfo> registeredModules = this.ModuleCatalog.Modules.ToDictionary(m => m.ModuleName);
  77. foreach (Lazy<IModule, IModuleExport> lazyModule in this.ImportedModules)
  78. {
  79. // It is important that the Metadata.ModuleType is used here.
  80. // Using GetType().Name would cause the Module to be constructed here rather than lazily when the module is needed.
  81. Type moduleType = lazyModule.Metadata.ModuleType;
  82. ModuleInfo registeredModule = null;
  83. if (!registeredModules.TryGetValue(lazyModule.Metadata.ModuleName, out registeredModule))
  84. {
  85. // If the module is not already in the catalog is it added.
  86. ModuleInfo moduleInfo = new ModuleInfo()
  87. {
  88. ModuleName = lazyModule.Metadata.ModuleName,
  89. ModuleType = moduleType.AssemblyQualifiedName,
  90. InitializationMode = lazyModule.Metadata.InitializationMode,
  91. State = lazyModule.Metadata.InitializationMode == InitializationMode.OnDemand ? ModuleState.NotStarted : ModuleState.ReadyForInitialization,
  92. };
  93. if (lazyModule.Metadata.DependsOnModuleNames != null)
  94. {
  95. moduleInfo.DependsOn.AddRange(lazyModule.Metadata.DependsOnModuleNames);
  96. }
  97. this.ModuleCatalog.AddModule(moduleInfo);
  98. }
  99. else
  100. {
  101. // If the module is already in the catalog then override the module type name from the imported module
  102. registeredModule.ModuleType = moduleType.AssemblyQualifiedName;
  103. }
  104. }
  105. this.LoadModulesThatAreReadyForLoad();
  106. }
  107. /// <summary>
  108. /// Checks if the module needs to be retrieved before it's initialized.
  109. /// </summary>
  110. /// <param name="moduleInfo">Module that is being checked if needs retrieval.</param>
  111. /// <returns>True if the module needs to be retrieved. Otherwise, false.</returns>
  112. protected override bool ModuleNeedsRetrieval(ModuleInfo moduleInfo)
  113. {
  114. return this.ImportedModules == null
  115. || !this.ImportedModules.Any(lazyModule => lazyModule.Metadata.ModuleName == moduleInfo.ModuleName);
  116. }
  117. }
  118. }