当前位置:网站首页>Talk about the modular system of ABP vNext

Talk about the modular system of ABP vNext

2020-12-08 14:29:32 Xinlai technology snow geese

  • Official website https://abp.io/
  • Open source https://github.com/abpframework/abp
  • EasyAbphttps://easyabp.io/
  • Abp modular https://abp.io/packages

Modular system

ABP vNext World outlook

stay Abp vNext Inside the frame , Modular system is the cornerstone of the whole framework , After understanding the modular system , It's easy to understand the rest of the design .

The modular system is like the LEGO toy shown above , A wonderful world is made up of scattered building blocks . Each block has a different shape , Different functions , Building blocks and building blocks are directly dependent on each other , Support each other .

There are two types of modules . They don't have any structural differences , It's just classified by function and purpose :

  • Framework module : These are the core modules of the framework , Like cache 、 mail 、 The theme 、 Security 、 serialize 、 verification 、Ef Core Integrate 、MongoDB Integrate ... wait . They don't have applications / Business function , But by providing a common infrastructure , Integration and abstraction will make your daily development easier .
  • Application module : These modules implement specific applications / Business function , image Blog 、 Document management 、 Identity management 、 Tenant management ... wait . It usually has its own entity , service ,API and UI Components .

How to use modules ?

Abp vNext How can the modules in the framework fit together like building blocks ?

There are only three steps in assembling modules :

  • First step : Establish direct dependencies of modules , Can pass DependsOnAttribute Properties to determine dependencies .
  • The second step : First configure the module , It can fill data and function settings for modules .
  • The third step : Use the functions provided by the module ( Interface ). Through the function interface to realize the module assembly .
public class Startup
{
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddApplication<AppModule>(); // Configure launched  Abp modular 
      }
      public void Configure(IApplicationBuilder app)
      {
          app.InitializeApplication(); // initialization  Abp modular 
      }
  }

How to customize the module ?

Abp vNext Specify that each module should define a module class and inherit AbpModule abstract class .

  • adopt DependsOnAttribute Feature to associate the modules that need to be used .
  • By rewriting OnApplicationInitialization Method to initialize the module . You can also configure AspNetCore Processing pipeline .
  • By rewriting ConfigureServices Method to configure the module .
[DependsOn(typeof(ModuleThree.ModuleThreeModule))]
[DependsOn(typeof(ModuleOne.ModuleOneModule))]
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
public class AppModule : AbpModule
{

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        //  Configure dependency injection 
    }

    public override void 
OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseConfiguredEndpoints();
    }
}

AbpModule Depth analysis

ABP The system only scans all modules by reflection when it starts up , Each module can pass through DependsOnAttribute Properties to determine dependencies , Using the topological sort algorithm , To determine the loading order of modules according to the dependency .( Load from the deepest modules in turn , Until all modules are started ).

AbpModule Provides some ways to manage the lifecycle of a module .

  • ConfigureServices: Is the main way to add your services to the dependency injection system and configure other modules .
  • OnApplicationInitialization: Initialize all services of all modules configured .
  • OnApplicationShutdown: If you want to execute when the application closes .
  • ... There are other methods that are not used much , The following flow chart will briefly explain .

The flow chart below is a brief description of AbpModule The order of execution between the methods provided .

AbpModule Class source code :

https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/AbpModule.cs

Take an example of regular email to illustrate the usefulness of these three methods .

  • A regular mail module is defined SendMailModule Module and inherit AbpModule .
  • stay ConfigureServices The sending time and address in the email address are configured , Mail module information, etc .
  • After configuring the module , stay OnApplicationInitialization Method to start the timer , Send email regularly .
  • When the system stops, the operation and maintenance personnel need to be reminded , stay OnApplicationShutdown Method to send an email to the operation and maintenance personnel .

ABP Modularity depends on the value process

The following log describes AbpModule The execution order between the methods provided in case of module dependency .

The demo code of the execution sequence in the figure above :

AbpModuleDemo.7z

summary

Abp vNext It's a modular design , Provides high scalability 、 High availability 、 Efficient development framework . In order to achieve high efficiency, we need to know how to use Abp All module functions in . Through the module design, you can quickly customize the function and better unit testing .

Abp vNext Module design is the direct adoption of Asp.NetCore The original function of , Expanded Startup.ConfigureServices and Startup.Configure Method , It is convenient for us to use directly when developing modules IServiceCollection and IApplicationBuilder.

Module design principles

  • A single principle . Each module has only one function , With clear boundaries , Achieve a high degree of decoupling and high reusability .
  • DDD Domain design . The domain boundary of the module is determined by the domain design idea , Avoid increasing complexity due to excessive modularization .

Module usage FAQ

  • Modularity is too detailed , It's easy to get into a maze of modules . This leads to a surge in learning costs .
  • Module function documentation is not clear , It's easy to use .
  • Too much dependence between modules , When you don't know the module design and function , It's hard to locate the problem .

quote

  • Abp vNext Source code analysis https://www.cnblogs.com/myzony/p/10722506.html
  • Official documents https://docs.abp.io/zh-Hans/abp/latest/Module-Development-Basics

Reprint is a power Sharing is a virtue

author : Ah ling

This article is from WeChat official account. - magiccodes(xl----0) , author :magiccodes

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-12-04

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Xinlai technology snow geese]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208142843093n.html