1. Introduction
ABP is an open source and document-complete application framework. In fact, it's not just a framework, considering its best practices, and ABP provides a powerful price model based on domain-driven design (DDD).
ABP supports the latest ASP. NET core and EF core, and also supports ASP. NET MVC 5.x and EF 6.x.
2. Quick look at an example
Public classTaskappservice:applicationservice, itaskappservice{Private ReadOnlyIrepository<task>_taskrepository; PublicTaskappservice (irepository<task>taskrepository) {_taskrepository=taskrepository; } [Abpauthorize (mypermissions.updatetasks)] Public AsyncTask updatetask (updatetaskinput input) {Logger.info ("Updating a task for input:"+input); varTask =await_taskrepository.firstordefaultasync (input. TASKID); if(Task = =NULL) { Throw NewUserfriendlyexception (L ("Couldnotfindthetaskmessage")); } input. Mapto (Task); }}
Above is an example of a application service method, DDD China's application service is invoked directly by the presentation layer to execute the application's use case.
Consider Updatetask as a way for JavaScript to be called through Ajax.
Advantages of 3.ABP
Dependency Injection (DI): ABP provides a traditional DI infrastructure, because this class is an application service, so it is usually registered as transient in the DI container (created on request). It can simply inject any dependency (for example, IRepository <Task> in this example).
Warehousing : ABP You can create a default repository for each entity (for example, IRepository <Task> in this example). The default repository has many useful methods, such as the FirstOrDefault method used in this example. We can extend the default repositories to meet our needs. Repositories abstract DBMS and ORM and simplify data access logic.
Authorization : The ABP can have declarative check permissions, and the ABP blocks access to the Updatetask method if the current user does not have the Update Tasks permission or is not logged in. ABP not only uses declarative attributes, but also some other authorization methods.
validation : The ABP automatically checks whether the input is null, and it validates all the attributes entered based on the standard data annotation properties and custom validation rules. If the request is not valid, an exception is thrown and processed on the client.
Audit log : Automatically saves the user, browser, IP address, calling service, method, parameters, call time, execution time, and other information for each request according to the Convention and configuration.
Unit of work : At the ABP, each application service method is assumed to be a unit of work by default. It automatically creates a connection at the beginning of the method and begins a transaction. If this method does not have any exception, the transaction is committed and the connection is freed. Although this method uses different warehousing or methods, they will all be atomic (transactional). When a transaction is committed, all changes to the entity are automatically saved. We don't even need to call _repository. Update (Task), as shown in the previous example.
exception Handling : In an ABP Web application, we almost never have to handle exceptions manually. By default, all exceptions will be handled automatically! If an exception occurs, the ABP automatically logs and returns the correct results to the client. For example, if this is an AJAX request, it returns a JSON object to the client to identify an error that occurred. Only in userfriendlyexception cases, it hides the real exception. It can also handle errors on the client and present the appropriate information to the user.
Log : We can use the Logger object defined in the base class. Log4net is used by default, but this can be changed to configurable.
localization : Notice that we used the ' L ' method when throwing an exception? In this way, it is automatically localized based on the culture of the current user.
Auto-mapping : In the last row, we use the Mapto extension method of the ABP to map the input attributes to the entity properties. It uses the AutoMapper library to perform the mapping. We can easily map attributes from one object to another based on a naming convention.
Dynamic API: Taskappservice is a simple class. Usually we have to write a wrapper API controller to expose the method to the JavaScript client, but the ABP will do it automatically at run time. In this way, we can use the application service method directly from the client.
Dynamic AJAX proxies : ABP creates proxy methods that make invoking an application service method as simple as invoking a JavaScript method on a client.
We can see the benefits of ABP in this simple class. All of these tasks usually take a long time, but in the ABP, the framework helps us. We just need to focus on our real business.
What ABP is ABP (ASP. Boilerplate)