WPF Prism framework (modular)

1 Introduction

  Modular development is an approach to program development that divides a program into a set of loosely coupled functional units (named modules) that can be integrated into larger applications. A module encapsulates a portion of an application's overall functionality and typically represents a set of related functions. It can include a range of related components, such as application functionality (including user interface and business logic) or application infrastructure (such as application-level services for logging or authenticating users). Modules are independent of each other but can communicate with each other loosely. Using modular application design, you can develop, test, deploy, and maintain applications more easily.

  For example, consider a personal banking application. Users can access a variety of features such as transferring funds between accounts, paying bills and updating personal information from a single user interface (UI). But behind the scenes, these functions are encapsulated in a discrete module. These modules communicate with each other and with back-end systems such as database servers and Web services. The application service integrates the different components in each different module and handles communication with the user. Users see an integrated view similar to a single application.

1.1 Benefits of building modular applications

  You're probably already building well-designed applications using components, interfaces, and classes, applying good object-oriented design principles. Even so, unless great care is taken, your application design may still be "monolithic" (within the application, all functionality is implemented in a tightly coupled manner), which can make the application difficult to develop, test, extend, and maintain.

  A modular app approach, on the other hand, helps you identify large functional areas of your app and allows you to develop and test that functionality independently. This can make development and testing easier, but also makes your application more flexible and easier to expand in the future. The benefit of a modular approach is that it makes your overall application architecture more flexible and maintainable because it allows you to break your application down into manageable parts. Each piece encapsulates specific functionality, and each piece is integrated through clear but loosely coupled communication channels.

1.2 Prism’s support for modular application development

  Prism provides support for modular application development and runtime module management within applications. Using Prism's modular development capabilities saves you time because you don't have to implement and test your own modular framework. Prism supports the following modular application development features:

  1. A module directory for registering named modules and the location of each module; you can create a module directory by
  2. By defining modules in code or Extensible Application Markup Language (XAML)
  3. By discovering modules in a directory you can load all modules without explicitly defining them in a centralized directory
  4. By defining the module in the configuration file
  5. Modules support declarative metadata attributes for initialization patterns and dependencies
  6. For module loading:

    • Dependency management, including duplication and loop detection to ensure modules are loaded in the correct order and loaded and initialized only once

    • Modules are downloaded on demand and in the background to minimize application startup time; remaining modules can be loaded and initialized in the background or when needed

  7. Integrate with dependency injection containers to support loose coupling between modules 

1.3 Core concepts, building blocks of modular applications

  This section introduces core concepts related to Prism modularity, including interfaces, module loading processes, module directories, communication between modules, and dependency injection containers.

2. Modular approach

      Modular to IModule interface

2.1 dll direct reference

First, introduce the Prism framework according to the method introduced in Prism (MVVM)

Then create the three projects shown in the diagram, introduce the Prism framework respectively, and create the corresponding folders

 This method needs to achieve modularity. ConfigureModuleCatalog cooperates with Profile (IModule) to manage the two dependent modules ModuleA and ModuleB in the ModuleWpf project.

Override the ConfigureModuleCatalog method in App.xaml.cs in MouleWpf

  protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {    
            moduleCatalog.AddModule<ModuleBProfile>();
            moduleCatalog.AddModule<ModuleAProfile>();
            base.ConfigureModuleCatalog(moduleCatalog);
        }   

 To call PageA and PageB, you need to register them. Only the registered interface can be used.

The registered class must inherit the IModule interface. This class is not written in the ModuleWpf project.

 public class ModuleBProfile : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {

        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<PageB, ModuleBProfile>();
        }
    }

The most important thing is to reference these two projects in ModuleWpf

 2.2 Specify path

The ModuleWpf project does not depend on ModuleA and ModuleB at all, and is completely decoupled.

Override the CreateModuleCatalog method in App.xaml.cs in MouleWpf

Set a search path for dependent modules 

  protected override IModuleCatalog CreateModuleCatalog()
        {
            return new DirectoryModuleCatalog() { ModulePath=@".\Modules"};
        }

Note that the module dll file is placed in this path

2.3 Through configuration files

Modify App.config under ModuleWpf through the configuration file

First create a custom node and use the configSections tag (note: only one configSections tag is allowed in each configuration file. If this element tag exists, it must also be the first child element under the root element (configuration))

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf"/>
	</configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
	<modules>
		<module assemblyFile="ModuleA.dll" moduleType="ModuleA.ModuleAProfile, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleAProfile" startupLoaded="True"/>
		<mosule assemblyFile="ModuleB.dll" moduleType="ModuleB.ModuleBProfile, ModuleB Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="ModuleBProfile" startupLoaded="True"/>
	</modules>
</configuration>

Override the CreateModuleCatalog method in App.xaml.cs in MouleWpf

   protected override IModuleCatalog CreateModuleCatalog()
        {
            return new ConfigurationModuleCatalog();
        }

Note that the returned value is different from 2.2

To implement the function, place the dll in the debug directory

 

Guess you like

Origin blog.csdn.net/qq_57212959/article/details/132021269