Concept
Modulecatalog is one of the main concepts in PRISM. It is mainly used to save the available modules of an application. Every module is defined by moduleinfo (including the module name, type, and location ).
Function implementation
Modulecatalog inherits from imodulecatalog. imodulecatalog declares the following attributes and methods:
- All modules saved by modulecatalog
IEnumerable<ModuleInfo> Modules { get; }
- Obtains the modules that a module depends on.
IEnumerable<ModuleInfo> GetDependentModules(ModuleInfo moduleInfo);
- Obtain all the modules that contain the modules parameter and its dependencies.
IEnumerable<ModuleInfo> CompleteListWithDependencies(IEnumerable<ModuleInfo> modules);
- Initialize modulecatalog. you can load modules and verify modules in this method.
void Initialize();
- Add a module to modulecatalog
void AddModule(ModuleInfo moduleInfo);
In addition to implementing the attributes and Methods declared by imodulecatalog, modulecatalog provides many other attributes and methods:
- Grouped modules and non-grouped modules are defined.
public IEnumerable<ModuleInfoGroup> Groups { get { return this.Items.OfType<ModuleInfoGroup>(); } } protected IEnumerable<ModuleInfo> GrouplessModules { get { return this.Items.OfType<ModuleInfo>(); } }
- Modules: Get all modules, including grouped and non-grouped
public virtual IEnumerable<ModuleInfo> Modules { get { return this.GrouplessModules.Union(this.Groups.SelectMany(g => g)); } }
- Provides two static methods for creating modulecatalog from XAML.
public static ModuleCatalog CreateFromXaml(Stream xamlStream)public static ModuleCatalog CreateFromXaml(Uri builderResourceUri)
- Verify modules:
-
- Check whether the module is repeated;
- Verify that modules does not have circular reference;
- Verify that the modules in different module groups do not reference each other;
- The module that verifies that initializationmode is whenavailable cannot depend on the module where mode is OnDemand. (The reason is that the loading time cannot conflict)
public virtual void Validate() { this.ValidateUniqueModules(); this.ValidateDependencyGraph(); this.ValidateCrossGroupDependencies(); this.ValidateDependenciesInitializationMode(); this.Validated = true; }
- Several addmodule methods are overloaded.
- Provides the method for adding a module group.
public virtual ModuleCatalog AddGroup(InitializationMode initializationMode, string refValue, params ModuleInfo[] moduleInfos)
Modulecatalog can also be used as a base class to customize catalog.
Application
There are two module catalog related methods in bootstrapper: Create and configure.
protected virtual IModuleCatalog CreateModuleCatalog() { return new ModuleCatalog(); } protected virtual void ConfigureModuleCatalog() { }
When Using Prism to build a WPF application, we generally customize a bootstrapper that inherits the prism unitybootstrapper or mefbootstrapper (of course, you can also use extension bootstrapper of other IOC container ).
In the defined bootstrapper, You need to override the configuremodulecatalog method and add the specific module to the module catalog.
protected override void ConfigureModuleCatalog() { base.ConfigureModuleCatalog(); ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog; moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule)); }
Summary
The overall implementation of modulecatalog is relatively simple and easy to understand. However, there is a very important algorithm mentioned in this article. I will write an article to introduce it separately.
This algorithm is used to verify the circular reference between modules.
Prism 5.0 source code reading modulecatalog