The architecture of ABP Framework and Module system explanation-Basic application

Source: Internet
Author: User

DDD tiering
in order to reduce complexity and improve the reusability of code, it is a widely accepted technology to adopt layered architecture.
To achieve tiered architecture, ABP follows the principle of DDD (Domain driven design), which is divided into four levels:

    • Presentation Layer (presentation): Provides a user interface to enable user interaction.
    • Application Layer (application): coordinates the presentation layer with the domain layer to coordinate business objects to perform specific application tasks. It does not contain business logic.
    • Domain layer (domain): Includes business objects and business rules, which is the core layer of the application.
    • Infrastructure Layer (infrastructure): provides universal technology to support a higher layer. For example, the storage of the infrastructure layer (Repository) enables database interaction through ORM.

Depending on your actual needs, there may be additional layers to add. For example:

Distributed service layer (distributed service): Used to expose application interfaces for remote client invocation. For example, through the ASP.net Web API and WCF.
These are common domain-centric hierarchical architectures. There may be subtle differences in the implementation of different projects.


Architecture of the ABP

A simple solution that consists of roughly 5 projects:

Each layer can be implemented with one or more assemblies.
1. Domain Layer (domain)
The domain layer is the business layer, the core of a project, and all business rules should be implemented at the domain level.
2. Entity (Entity)
Entities represent the data and operations of the business domain, in practice, by mapping to database tables.
3. Warehousing (Repository)
Warehousing is used to manipulate databases for data access. The warehousing interface is defined in the domain layer, and the implementation class of the warehousing should be written at the infrastructure level.
4. Domain Services (domain service)
When the business rules that are processed span two (or more) entities, they should be written in the domain service method.
5. Field events (Domain event)
Domain events can be triggered when certain situations occur in the domain layer, and they are captured and processed at the appropriate place.
6. Work module (unit of Work)
A work unit is a design pattern that maintains a list of business objects that have been modified (such as additions, deletions, updates, and so on). It is responsible for coordinating the persistence of these business objects and concurrency issues.


Application Layer (application)
The application tier provides a number of application service (application services) methods for presentation layer invocation. An application service method receives a DTO (data transfer object) as an input parameter, performs a specific domain-level operation using this input parameter, and returns another dto as needed. Between the presentation layer and the realm layer, the entity (Entity) object should not be received or returned, and the DTO mapping should be performed. An application service method is usually considered to be a unit of work (Work). Validation of user input parameters should also be implemented at the application level. ABP provides an infrastructure that makes it easy to implement input parameter validation. It is recommended to use a tool such as automapper to map the entity to the DTO.

infrastructure Layer (infrastructure)
When warehousing interfaces are defined in the domain layer, these interfaces should be implemented in the infrastructure layer. You can use ORM tools, such as EntityFramework or NHibernate. The base class for ABP already provides support for both ORM tools. Database migrations are also used for this layer.

Web and Presentation layer (Web & presentation)
The Web layer is implemented using ASP.net MVC and the Web API. Can be used for multiple page applications (MPA) and single page applications (SPA).
At Spa, all resources are loaded into the client browser one at a time (or only core resources are loaded first, other resources are lazy), and then the server-side Webapi interface gets the data through Ajax, and then the HTML code is generated from the data. The entire page will not be refreshed. Now there are a lot of spa JS framework, such as: Angularjs, Durandaljs, Backbonejs, Emberjs. ABP can use any similar front-end framework, but ABP provides a number of help classes that make it easier to use ANGULARJS and Durandaljs.
In classic multiple-page applications (MPA), the client issues a request to the server, and the server-side code (the 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 display. Every new page that appears will be refreshed throughout the page.
Spa and MPA involve completely different architectures and different scenarios. A management background for the use of spa, blog is more suitable for MPA, because it is more conducive to be crawled by search engines.
SIGNALR is a perfect tool for sending push notifications from the server to the client. It can provide users with a rich real-time experience.
There are already many client JavaScript frameworks or libraries, and jquery is one of the most popular, and it has thousands of free plug-ins. Using bootstrap can make it easier for us to write HTML and CSS work.
ABP also implements a code function that automatically creates JavaScript based on the Web API interface to simplify the invocation of JS to the Web API. Also have the server side of the menu, language, settings, etc. to the JS end. (But in my own project, I turned these automatic generation features off, because the need is not great, and these will affect performance).
ABP automatically handles the exceptions returned by the server side and prompts the user with a friendly interface.


ABP Module System
The ABP Framework provides the basis for creating and assembling modules, and one module can rely on another module. In general, an assembly can be viewed as a module. In the ABP framework, a module is defined by a class that inherits from Abpmodule.
If you have studied orchard's friends, you should know that module modules are powerful. The nature of the module is reusability, you can call anywhere, and by implementing the module, you can write the module to others.
Assembly Assembly: Assembly is a collection of information including the name, version number, self-describing, file association, and file location of the program. The simplest understanding is that a DLL generated by your own class library can be viewed as an assembly that can include many classes, including many methods.
. NET can get the classes and methods in an assembly through reflection.
In the following example, we develop a module that can be called mybolgapplication in a number of different applications, and the code is as follows:

public class Myblogapplicationmodule:abpmodule//Definition
{public
 override void Initialize ()//initialization
 {
 Iocmanager.registerassemblybyconvention (assembly.getexecutingassembly ());
 This line of code is basically the same. Its role is to register a specific class or interface of the current assembly into a dependency injection container.
 }
}

The ABP framework scans all assemblies and discovers all the classes in the Abpmodule class that have been imported, and if you have created an application that contains multiple assemblies, for ABP, our recommendation is to create a module for each assembly.
Life-Cycle Events
In one application, the ABP framework invokes some of the specified methods of module modules to start and close the module. We can overload these methods to accomplish our own tasks.
The ABP framework invokes these methods in the order of dependencies, and if: Module a relies on module B, module B is initialized before module A, and the method in which the module starts is as follows:

    • Preinitialize-b
    • Preinitialize-a
    • Initialize-b
    • Initialize-a
    • Postinitialize-b
    • Postinitialize-a

The following is a description of the specific method:

1.PreInitialize
Pre-initialization: This method is invoked the first time after the application is started. Before relying on injection registration, you can specify your own special code in this method. For example: If you create a traditional registration class, then you have to register this class (using Iocmanager to register the registration Class), you can register the event to the IOC container. Wait
2.Initialize
Initialization: In this method it is generally used to register dependency injection, which is generally achieved by iocmanager.registerassemblybyconvention this method. If you want to implement a custom dependency injection, refer to the documentation for dependency injection.
3.PostInitialize
Commit initialization: The last method, which is used to resolve dependencies.
4.Shutdown
Off: This method is invoked when the application is closed.

Modular Dependencies (module dependencies)
the ABP framework automatically resolves dependencies between modules, but we recommend that you explicitly declare dependencies by overloading the Getdependencies method.

[DependsOn (typeof (Myblogcoremodule))]//defines dependencies public class Myblogapplicationmodule:abpmodule by annotations (
{
 public override void Initialize ()
 {
  iocmanager.registerassemblybyconvention (assembly.getexecutingassembly ());
 }
}

For example, the above code, we declare the dependencies of Myblogapplicationmodule and myblogcoremodule (through attribute attributes), Myblogapplicationmodule This application module relies on the Myblogcoremodule core module, and the Myblogcoremodule core module is initialized before the Myblogapplicationmodule module.
 
How to customize the module method
There may be methods in our own defined modules that are called by other modules that depend on the current module, and the following example assumes Module 2 is dependent on Module 1, and want to call the method of Module 1 in the pre initialization.

public class Mymodule1:abpmodule
{public
 override void Initialize ()//initialization module
 {
 Iocmanager.registerassemblybyconvention (assembly.getexecutingassembly ());//here, register for Dependency injection.
 }
 public void MyModuleMethod1 ()
 {
 //Here is a custom method written 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 MyModuleMethod1 method.
 }
 public override void Initialize ()
 {
 iocmanager.registerassemblybyconvention ( Assembly.getexecutingassembly ());
 }

In this way, Module 1 is injected into Module 2, so Module 2 can invoke the method of Module 1.

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.