Prism 5.0 source code reading modulecatalog

Source: Internet
Author: User


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:
    1. Check whether the module is repeated;
    2. Verify that modules does not have circular reference;
    3. Verify that the modules in different module groups do not reference each other;
    4. 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.


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));        }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.