Prism Modularity With Unity in WPF Using MVVM

Prism Modularity: This topic shows how to create a modular WPF application using the Prism library. The samples show how to code, discover and initialize modules.

Creating modules: Modules are classes that implement the IModule interface. Declarative attributes can be used to name modules, control initialization, and define dependencies.

Registering modules: Modules can be registered in the following ways:

  • Directly in code: Modules can be directly registered in the module catalog in the application code. Using this approach, you can use conditional logic to determine which module should be included in your application. Modules added in the code are referenced by the application instead of being loaded at run time.
  • Using configuration: Prism can register modules with the module catalog by loading a configuration file. Declaring the modules in configuration allows the modules to be loaded and initialized independently of the application.
  • Using directory inspection: A directory can be specified and inspected to load assemblies in the directory and discover modules.
  • Registering module dependencies: Modules can have dependencies on other modules. Prism provides dependencies management, including cyclic dependencies and duplicate module detection.
Initializing modules: Prism supports the following two initialization modes:
  1. When available: Modules can be initialized as soon as they are available. Modules downloaded the application are initialized during startup. Modules set to download in the background are initialized immediately after downloading completes.
  2. On-demand: Modules can be initialized when the application code requests it. Modules downloaded in the background start downloading when the application requests the module and then they initialize immediately after downloading completes.
In this example, I have taken 3 class libraries and one WPF Application. See the screen below.



Here every class library project is implementing “IModule”. See the code below.
  1. using Microsoft.Practices.Prism.Modularity;  
  2. using Microsoft.Practices.Prism.Regions;  
  3. namespace Module1  
  4. {  
  5.     /// <summary>  
  6.     ///   
  7.     /// </summary>  
  8.     public class Module1 : IModule  
  9.     {  
  10.   
  11.         #region Constructors  
  12.    
  13.         /// <summary>  
  14.         ///   
  15.         /// </summary>  
  16.         public Module1(IRegionManager iRegionManager)  
  17.         {   
  18.             this.iRegionManager = iRegionManager;  
  19.         }  
  20.   
  21.         #endregion  
  22.    
  23.         /// <inheritdoc/>  
  24.         #region IModule Members  
  25.  
  26.     public void Initialize()  
  27.     {  
  28.             this.iRegionManager .RegisterViewWithRegion("Module1",typeof(Views.Module1Control));  
  29.       
  30.     }  
  31.      
  32.         #endregion  
  33.   
  34.         #region Instance Fields  
  35.   
  36.     /// <summary>  
  37.     ///   
  38.     /// </summary>  
  39.     private IRegionManager iRegionManager;  
  40.          
  41.          #endregion  
  42.   
  43.     }  
  44. }  
Initializing Modules: Here I have initialized modules in every IModule Implementation class, see the code below.
  1. /// <inheritdoc/>  
  2. public void Initialize()  
  3. {  
  4.     this  
  5.         .iRegionManager  
  6.         .RegisterViewWithRegion("Module3Control1"typeof(Views.Module3Control));  
  7.    
  8.     this  
  9.         .iRegionManager  
  10.         .RegisterViewWithRegion("Module3Control2"typeof(Views.Module3Control2));  
  11. }  
Registering Modules: Here I have registered all modules in Bootstrapper class, see the code below.
  1. /// <inheritdoc/>  
  2. protected override void ConfigureModuleCatalog()  
  3. {   
  4.     ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;  
  5.     moduleCatalog.AddModule(typeof(Module1.Module1));  
  6.     moduleCatalog.AddModule(typeof(Module2.Module2));  
  7.     moduleCatalog.AddModule(typeof(Module3.Module3));  
  8. }  
  9.   
  10. Finally, I have read all Modules in shell.xaml file, see the code below.  
  11. <ItemsControl Grid.Row="0"  
  12.                       Grid.Column="0"  
  13.                       prism:RegionManager.RegionName="Module1" />  
  14.         <ItemsControl Grid.Row="0"  
  15.                       Grid.Column="1"  
  16.                       prism:RegionManager.RegionName="Module2" />  
  17.         <ItemsControl Grid.Row="1"  
  18.                       Grid.Column="0"  
  19.                       prism:RegionManager.RegionName="Module3Control1" />  
  20.         <ItemsControl Grid.Row="1"  
  21.                       Grid.Column="1"  
  22.                       prism:RegionManager.RegionName="Module3Control2" />  
See the screen shot below.

Up Next
    Ebook Download
    View all
    Learn
    View all