In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about what ABP is. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
ABP is the abbreviation of "ASP.NET Boilerplate Project (ASP.NET template Project)".
ASP.NET Boilerplate is a new starting point for developing modern WEB applications with best practices and popular technologies, and it aims to become a common WEB application framework and project template.
ABP's official website: http://www.aspnetboilerplate.com
ABP's open source project on Github: https://github.com/aspnetboilerplate
The Origin of ABP
"DRY-- avoids duplicating code" is one of the most important ideas that a good developer has when developing software. We all have some similar requirements when developing enterprise WEB applications, such as login pages, user / role management, rights verification, data validation, multilingual / localization, and so on. A high-quality large software will apply some best practices, such as hierarchical architecture, domain-driven design, dependency injection, and so on. We may also use tools such as ORM, database migration (Database Migrations), logging (Logging), and so on.
Creating an enterprise application from scratch is tedious because you need to do a lot of common basic work over and over again. Many companies are developing their own application frameworks to reuse in different projects, and then develop some new functions based on the framework. But not every company has such strength. If we can share more, we may be able to avoid rewriting similar code for every company or project. The author named the project "ASP.NET Boilerplate" in the hope that it can become a new starting point for the development of general enterprise WEB applications, and directly take ABP as the project template.
What is ABP?
ABP is a starting point for using best practices and the most popular tools for new modern Web applications. Can be used as a basic framework or project template for general-purpose applications. Its functions include:
Server side:
Based on the latest .NET technologies (currently ASP.NET MVC 5, Web API 2, C # 5.0, which will be upgraded after the official release of ASP.NET 5)
Implement domain-driven design (entities, warehousing, domain services, domain events, application services, data transfer objects, units of work, etc.)
Implementing hierarchical architectures (domain layer, application layer, presentation layer, and infrastructure layer) provides an infrastructure for developing reusable and configurable modules to integrate some of the most popular open source frameworks / libraries, perhaps some of which you are using.
Provides an infrastructure that makes it easy to use dependency injection (using Castle Windsor as a container for dependency injection)
Provide Repository warehousing mode to support different ORM (Entity Framework, NHibernate, MangoDb and in-memory database have been implemented)
Support and implement database migration (Code first of EF) modular development (each module has an independent EF DbContext, which can specify the database separately)
Includes a simple and flexible multilingual / localization system
Includes an EventBus to implement unified exception handling for global domain events on the server side (the application layer hardly needs to handle its own exception handling code)
Data validity verification (Asp.NET MVC can only verify the parameters of Action method, ABP implements the parameter validity verification of Application layer method)
Automatically create Web Api layers through Application Services (no need to write ApiController layers)
Providing base classes and helper classes allows us to easily implement some common tasks
Use the "convention over configuration principle"
Client:
Bootstrap, Less, AngularJs, jQuery, Modernizr and other JS libraries: jQuery.validate, jQuery.form, jQuery.blockUI, json2, etc.
Project templates are provided for single-page applications (AngularJs, Durandaljs) and multi-page applications (Bootstrap+Jquery).
Automatically create a proxy layer for Javascript to make it easier to use Web Api to encapsulate some Javascript functions, to use ajax, message boxes, notification components, busy mask layers, and so on
In addition to the ABP framework project, a module called "Zero" has been developed to implement the following functions:
Authentication and authorization management (implemented through ASP.NET Identity)
User-role management system sets access management (system level, tenant level, user level, scope automatic management)
Audit log (automatically records callers and parameters for each interface)
ABP is not what?
ABP provides an application development model for best practices. It has basic classes, interfaces, and tools that make it easy for us to build maintainable large-scale applications.
However:
It is not one of the RAD tools, and the purpose of the RAD tool is to create applications without coding. Instead, ABP provides a coding best practice.
It is not a code generation tool. Although it has some features to build dynamic code at run time, it cannot generate code.
It is not an integrated framework. Instead, it uses popular tools / libraries to accomplish specific tasks (such as using EF for ORM, using Log4Net for logging, making Castle Windsor as a dependency injection container, and AngularJs for the SPA framework).
From my experience of using ABP for a few months, although ABP is not a RAD, developing projects with it is definitely much faster than the traditional three-tier architecture.
Although ABP is not a code generation tool, it makes the code of our project more concise and standardized, which facilitates the use of code generation tools.
I use VS2013's Scaffolder+T4 code generator, which can automatically generate all the front and back end code and database according to the UML class diagram of the domain object. The simple CURD module hardly needs to write code, and the module with complex business logic can mainly supplement the domain layer code. In this way, you can spend more time on the design of the domain model and reduce the time of writing code.
The following is an example of how to use ABP to develop a project through the original author's "simple task system" example.
Create an empty web application from a template
ABP provides a startup template for new projects (although you can create the project manually and get the ABP package from nuget, the template is easier).
Go to www.aspnetboilerplate.com/Templates to create your application from the template.
You can choose SPA (AngularJs or DurandalJs) or the MPA (classic multi-page application) project. You can choose Entity Framework or NHibernate as the ORM framework.
Here we select AngularJs and Entity Framework, fill in the project name "SimpleTaskSystem", click the "CREATE MY PROJECT" button to download a zip package, unzip it and get the VS2013 solution, using .NET version 4.5.1.
Abp components and other third-party components are referenced in each project and need to be downloaded from Nuget.
The yellow exclamation mark icon indicates that this component does not exist in the local folder and needs to be restored from the Nuget. Do the following:
To get the project up and running, you also have to create a database. This template assumes that you are using SQL2008 or newer versions. Of course, it can also be easily replaced with other relational databases.
Open the Web.Config file to view and configure the link string:
(when Code first data migration for EF is used later, a database named SimpleTaskSystemDb is automatically created in the SQL Server database. )
In this way, the project is ready to run! Open VS2013 and press F5:
This simple task system program will be implemented step by step.
Create solid
Write entity classes in the Core project because entities are part of the domain layer.
A simple application scenario: create tasks (tasks) and assign them to people. We need both Task and Person entities.
The Task entity has several attributes: description (Description), creation time (CreationTime), task status (State), and optional navigation attribute (AssignedPerson) to reference the Person.
Public class Task: Entity {ForeignKey ("AssignedPersonId")] public virtual Person AssignedPerson {get; set;} public virtual int? AssignedPersonId {get; set;} public virtual string Description {get; set;} public virtual DateTime CreationTime {get; set;} public virtual TaskState State {get; set;} public Task () {CreationTime = DateTime.Now; State = TaskState.Active;}}
The Person entity is simpler, defining only one Name attribute:
Public class Person: Entity {public virtual string Name {get; set;}}
In the ABP framework, there is an Entity base class that has an Id attribute. Because the Task class inherits from Entity, it has an Id of type long. The Person class has an Id of type int because the int type is the default type of the Entity base class Id, and when no specific type is specified, the Id of the entity is the int type.
Create DbContext
To use EntityFramework, you need to define the DbContext class first. The template of ABP has created the DbContext file. We just need to add the Task and Person classes to IDbSet. Please see the code:
Public class SimpleTaskSystemDbContext: AbpDbContext {public virtual IDbSet Tasks {get; set;} public virtual IDbSet People {get; set;} public SimpleTaskSystemDbContext (): base ("Default") {} public SimpleTaskSystemDbContext (string nameOrConnectionString): base (nameOrConnectionString) {}}
Create database tables through Database Migrations
We use EntityFramework's Code First schema to create a database schema. The project generated by ABP template has the data migration feature enabled by default. We modify the Configuration.cs file under the Migrations folder under the SimpleTaskSystem.EntityFramework project:
Internal sealed class Configuration: DbMigrationsConfiguration {public Configuration () {AutomaticMigrationsEnabled = false;} protected override void Seed (SimpleTaskSystem.EntityFramework.SimpleTaskSystemDbContext context) {context.People.AddOrUpdate (p = > p.Name, new Person {Name = "Isaac Asimov"}, new Person {Name = "Thomas More"}, new Person {Name = "George Orwell"}, new Person {Name = "Douglas Adams"});}}
In the package Manager console window at the bottom of VS2013, select the default project and execute the command "Add-Migration InitialCreate"
A xxxx-InitialCreate.cs file is generated under the Migrations folder, with the following contents:
Public partial class InitialCreate: DbMigration {public override void Up () {CreateTable ("dbo.StsPeople", c = > new {Id = c.Int (nullable: false, identity: true), Name = c.String (),}) .PrimaryKey (t = > t.Id) CreateTable ("dbo.StsTasks", c = > new {Id = c.Long (nullable: false, identity: true), AssignedPersonId = c.Int (), Description = c.String (), CreationTime = c.DateTime (nullable: false), State = c.Byte (nullable: false),}) .PrimaryKey (t = > t.Id) .ForeignKey ("dbo.StsPeople", t = > t.AssignedPersonId) .Index (t = > t.AssignedPersonId) } public override void Down () {DropForeignKey ("dbo.StsTasks", "AssignedPersonId", "dbo.StsPeople"); DropIndex ("dbo.StsTasks", new [] {"AssignedPersonId"}); DropTable ("dbo.StsTasks"); DropTable ("dbo.StsPeople");}}
Then continue to execute "Update-Database" in the package Manager console, which automatically creates the corresponding data table in the database:
PM > Update-Database
The database is shown as follows:
(after modifying the entity, you can execute Add-Migration and Update-Database again, and you can easily synchronize the database structure with the entity class.)
Define warehousing interface
Through the warehousing mode, we can better separate the business code from the database operation code, and we can have different implementation classes for different databases, but the business code does not need to be modified.
The code that defines the warehousing interface is written to the Core project because the warehousing interface is part of the domain layer.
Let's first define the warehousing interface of Task:
Public interface ITaskRepository: IRepository {
It inherits from the IRepository generic interface in the ABP framework.
Common addition, deletion, modification and search methods have been defined in IRepository:
So ITaskRepository has the above methods by default. You can add its unique method GetAllWithPeople (...).
There is no need to create a repository class for the Person class, because the default method is sufficient. ABP provides a way to inject generic repositories, as you'll see in the TaskAppService class in the section "creating Application Services" later.
Implement the warehouse class
We will implement the ITaskRepository warehousing interface defined above in the EntityFramework project.
The project created through the template has defined a repository base class: SimpleTaskSystemRepositoryBase (this is a good practice because you can add general methods to this base class later).
Public class TaskRepository: SimpleTaskSystemRepositoryBase, ITaskRepository {public List GetAllWithPeople (int? AssignedPersonId, TaskState? State) {/ / in the warehousing method, the ABP framework does not have to deal with database connections, DbContext, and data transactions. Var query = GetAll (); / / GetAll () returns an IQueryable interface type / / add some Where conditions if (assignedPersonId.HasValue) {query = query.Where (task = > task.AssignedPerson.Id = = assignedPersonId.Value);} if (state.HasValue) {query = query.Where (task = > task.State = = state) } return query .OrderByDescending (task = > task.CreationTime) .include (task = > task.AssignedPerson) .ToList ();}}
TaskRepository inherits from SimpleTaskSystemRepositoryBase and implements the ITaskRepository interface defined above.
Create an application service (Application Services)
Define the application service in the Application project. First, define the interface of the application service layer of Task:
Public interface ITaskAppService: IApplicationService {GetTasksOutput GetTasks (GetTasksInput input); void UpdateTask (UpdateTaskInput input); void CreateTask (CreateTaskInput input);}
ITaskAppService inherits from IApplicationService,ABP to automatically provide some functional features (such as dependency injection and parameter validation) for this class.
Then, we write the TaskAppService class to implement the ITaskAppService interface:
Public class TaskAppService: ApplicationService, ITaskAppService {private readonly ITaskRepository _ taskRepository; private readonly IRepository _ personRepository; / the constructor automatically injects the class or interface we need / public TaskAppService (ITaskRepository taskRepository, IRepository personRepository) {_ taskRepository = taskRepository; _ personRepository = personRepository;} public GetTasksOutput GetTasks (GetTasksInput input) {/ / call the specific method of Task repository GetAllWithPeople var tasks = _ taskRepository.GetAllWithPeople (input.AssignedPersonId, input.State) / automatically convert List to List return new GetTasksOutput {Tasks = Mapper.Map (tasks)} with AutoMapper;} public void UpdateTask (UpdateTaskInput input) {/ / can Logger directly, which is the Logger.Info defined in the ApplicationService base class ("Updating a task for input:" + input) / obtain the Task entity object var task = _ taskRepository.Get (input.TaskId) of the specified Id through the general method Get of the warehouse base class; / / modify the attribute value of the task entity if (input.State.HasValue) {task.State = input.State.Value;} if (input.AssignedPersonId.HasValue) {task.AssignedPerson = _ personRepository.Load (input.AssignedPersonId.Value) } / / none of us need to call the Update method / / because the method in the application service layer turns on the unit of work mode (Unit of Work) by default / / the ABP framework automatically saves all changes to the entity when the unit of work is complete, unless an exception is thrown. It will be rolled back automatically when there is an exception, because the database transaction is turned on by default in the unit of work. } public void CreateTask (CreateTaskInput input) {Logger.Info ("Creating a task for input:" + input); / / create a new Task entity var task = new Task {Description = input.Description} by entering parameters; if (input.AssignedPersonId.HasValue) {task.AssignedPersonId = input.AssignedPersonId.Value;} / / call the Insert method of the repository base class to save the entity to the database _ taskRepository.Insert (task);}}
TaskAppService uses warehousing for database operations, which leads to constructor injection references to the repository object.
Data verification
If the parameter object of the application service (Application Service) method implements the IInputDto or IValidate interface, ABP automatically verifies the validity of the parameters.
The CreateTask method has a CreateTaskInput parameter, which is defined as follows:
Public class CreateTaskInput: IInputDto {public int? AssignedPersonId {get; set;} [Required] public string Description {get; set;}}
The Description attribute specifies that it is required through annotations. Other Data Annotation features can also be used.
If you want to use custom authentication, you can implement the ICustomValidate interface:
Public class UpdateTaskInput: IInputDto, ICustomValidate {[Range (1, long.MaxValue)] public long TaskId {get; set;} public int? AssignedPersonId {get; set;} public TaskState? State {get; set;} public void AddValidationErrors (List results) {if (AssignedPersonId = = null & & State = = null) {results.Add (new ValidationResult ("AssignedPersonId and State cannot be empty at the same time!", new [] {"AssignedPersonId", "State"});}
You can write custom validation code in the AddValidationErrors method.
Create a Web Api service
ABP can easily publish the public method of Application Service as a Web Api interface, which can be called by clients through ajax.
DynamicApiControllerBuilder .ForAll (Assembly.GetAssembly (typeof (SimpleTaskSystemApplicationModule)), "tasksystem") .build ()
All classes in the SimpleTaskSystemApplicationModule assembly that inherit the IApplicationService interface will automatically create a corresponding ApiController, and the exposed methods will be converted to WebApi interface methods.
It can be called through a routed address such as http://xxx/api/services/tasksystem/Task/GetTasks.
Through the above case, this paper briefly introduces the usage of domain layer, infrastructure layer and application service layer.
Now you can call the Application Service method directly in the Action method of ASP.NET MVC's Controller.
If you use SPA single-page programming, you can directly call the corresponding Application Service method on the client side through ajax (by creating a dynamic Web Api).
Thank you for reading! This is the end of this article on "what is ABP?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.