The architecture and Module System of the ABP framework, and the structure of the abp System

Source: Internet
Author: User

The architecture and Module System of the ABP framework, and the structure of the abp System

DDD hierarchy
To reduce complexity and improve code reusability, adopting a layered architecture is a widely accepted technology.
In order to achieve a layered architecture, ABPS follow the principle of DDD (domain-driven design) and are divided into four layers:

  • Presentation: provides a user interface for user interaction.
  • Application Layer: coordinates the presentation layer and domain layer, and coordinates business objects to execute specific Application tasks. It does not contain business logic.
  • Domain: Includes Business Objects and business rules. This is the core layer of the application.
  • Infrastructure layer (Infrastructure): provides general technology to support higher layers. For example, the infrastructure layer storage (Repository) can implement database interaction through ORM.

An additional layer may be added as needed. For example:

Distributed Service layer (Distributed Service): used to publish application interfaces for remote clients to call. For example, it is implemented through ASP. NET Web API and WCF.
These are common domain-centered hierarchical architectures. Different projects may have slight differences in implementation.


The architecture of ABP

A simple solution consists of five projects:

Each layer can be implemented using one or more sets.
1. Domain)
The domain layer is the business layer and the core of a project. All business rules should be implemented at the domain layer.
2. Entity)
Entities represent data and operations in the business field. In practice, they are used to map to database tables.
3. Repository)
Warehousing is used to operate databases for data access. The warehousing interface is defined at the domain layer, and the warehousing implementation class should be written at the infrastructure layer.
4. Domain service)
When the business rules to be processed span two (or more) entities, they should be written in the domain service method.
5. Domain events)
Domain events can be triggered when certain situations occur at the domain layer, and they are captured and processed in corresponding places.
6. Unit of Work)
A work unit is a design pattern used to maintain a list of business objects that have been modified (such as adding, deleting, and updating. It is responsible for coordinating the persistence work and concurrency issues of these business objects.


Application)
The Application layer provides some Application Services methods for the presentation layer to call. An application service method receives a DTO (data transmission object) as the input parameter, uses this input parameter to perform specific domain layer operations, and returns another DTO as needed. DTO ing should be performed between the presentation layer and the domain layer, instead of receiving or returning Entity objects. An application service method is generally considered as a Unit of Work ). The verification of user input parameters should also be implemented at the application layer. ABC provides an infrastructure that allows us to easily validate input parameters. We recommend that you use a tool like AutoMapper to map objects to DTO.
 
Infrastructure layer (Infrastructure)
When the warehousing interfaces are defined in the domain layer, these interfaces should be implemented in the infrastructure layer. You can use an ORM tool, such as EntityFramework or nhib.pdf. The basic class of the ABP has provided support for these two ORM tools. Database migration is also used at this layer.
 
WEB & Presentation)
The Web layer is implemented using ASP. net mvc and Web APIs. It can be used for multi-page applications (MPA) and single-page applications (SPA) respectively ).
In the SPA, all resources are loaded to the client browser at one time (or only core resources are loaded first, and other resources are loaded lazily), and then data is obtained by calling the WebApi interface of the server through AJAX, then generate HTML code based on the data. Does not refresh the entire page. There are already many spa js frameworks, such as AngularJs, DurandalJs, BackboneJs, and EmberJs. You can use any similar front-end framework. However, you can use AngularJs and DurandalJs more easily by providing some help classes.
In the classic multi-page application (MPA), the client sends a request to the server, the server code (ASP. net mvc Controller) obtains data from the database, and generates HTML using the Razor view. These generated HTML pages are sent back to the client for display. The entire page is refreshed every time a new page is displayed.
SPA and MPA involve completely different architectures and have different application scenarios. A management background is suitable for SPA, and a blog is more suitable for MPA, because it is more conducive to crawling by search engines.
SignalR is a perfect tool for sending push notifications from the server to the client. It provides users with rich real-time experience.
There are already many client-side Javascript frameworks or libraries. JQuery is the most popular among them, and it has thousands of free plug-ins. Using Bootstrap makes it easier for us to write Html and CSS.
You can also create Javascript code functions automatically based on Web API interfaces to simplify Javascript calls to Web APIs. In addition, the menu, language, and settings of the server are generated to the JS end. (However, in my own projects, I disable these auto-generated functions because the necessity is not great and these will affect the performance ).
The ABP automatically handles exceptions returned by the server and prompts the user on a friendly interface.


ABP Module System
The ABP framework provides the basis for creating and assembling modules. One module can depend on another module. Generally, an assembly can be regarded as a module. In the ABP framework, a module is defined by a class, which inherits from the AbpModule.
Note: If you have learned Orchard, you should know that the module is powerful. The essence of a module is reusability. You can call it anywhere, and through the implementation module, the module you write can also be used by others.
Assembly: Assembly is a set of information including the program name, version number, self-description, file association, and file location. The simplest understanding is: a dll generated by a class library you write can be considered as an assembly. This assembly can include many classes and many methods.
. Net can get classes and methods in an assembly through reflection.
In the following example, the MybolgApplication module can be called in multiple different applications. The Code is as follows:

Public class MyBlogApplicationModule: AbpModule // defines {public override void Initialize () // initialization {IocManager. registerAssemblyByConvention (Assembly. getExecutingAssembly (); // This line of code is basically unchanged. It registers the specific classes or interfaces of the current Assembly to the dependency injection container. }}

The ABP framework scans all the assemblies and finds all imported classes in the AbpModule class. If you have created an application that contains multiple assemblies, we recommend that you create a Module for each assembly ).
Lifecycle events
In an application, the abp Framework calls some specified methods of the Module to start and close the Module. We can reload these methods to complete our own tasks.
The ABP Framework calls these methods in the order of dependency. If module A depends on Module B, Module B should be initialized before module A. The order of the methods for starting the module is as follows:

  • PreInitialize-B
  • PreInitialize-
  • Initialize-B
  • Initialize-
  • PostInitialize-B
  • PostInitialize-

The following describes the specific methods:

1. PreInitialize
Pre-initialization: This method is called for the first time after the application is started. Before registering dependency injection, you can specify your own special code in this method. For example: if you have created a traditional registration class, you must register this class first (register the registration class using IocManager). You can register the event to the IOC container. .
2. Initialize
Initialization: This method is generally used to register dependency injection. We generally use IocManager. RegisterAssemblyByConvention to implement this method. If you want to implement custom dependency injection, refer to the dependency injection documentation.
3. PostInitialize
Submit initialization: The last method used to parse dependencies.
4. Shutdown
Close: after the application is closed, this method is called.
 
Module dependencies)
The Abp framework automatically parses the dependencies between modules. However, we recommend that you declare the Dependencies by reloading the GetDependencies method.

[DependsOn (typeof (MyBlogCoreModule)] // define the dependency public class MyBlogApplicationModule: AbpModule {public override void Initialize () {IocManager. registerAssemblyByConvention (Assembly. getExecutingAssembly ());}}

For example, in the above Code, we declare the dependency between MyBlogApplicationModule and MyBlogCoreModule (through attribute). The application module of MyBlogApplicationModule depends on the core module of MyBlogCoreModule, the core module of MyBlogCoreModule is initialized before the module of MyBlogApplicationModule.
 
How to customize the Module Method
A method can be called by other modules that depend on the current module. In the following example, we assume that module 2 depends on Module 1, in addition, you want to call the method of Module 1 during pre-initialization.

Public class MyModule1: AbpModule {public override void Initialize () // initialization module {IocManager. RegisterAssemblyByConvention (Assembly. GetExecutingAssembly (); // register dependency injection here. } Public void MyModuleMethod1 () {// write the custom method here. } [DependsOn (typeof (MyModule1)] public class MyModule2: AbpModule {private readonly MyModule1 _ myModule1; public MyModule2 (MyModule1 myModule1) {_ myModule1 = myModule1 ;} public override void PreInitialize () {_ myModule1.MyModuleMethod1 (); // call the MyModuleMethod1 method. } Public override void Initialize () {IocManager. RegisterAssemblyByConvention (Assembly. GetExecutingAssembly ());}}

In this way, Module 1 is injected into Module 2. Therefore, Module 2 can call the method of Module 1.

Related Article

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: info-contact@alibabacloud.com 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.