ABP applicability transformation – simplify the project structure generated by ABP cli

Time:2021-5-7

Overview

Both companies and individuals have different development habits. By establishing project templates, developers can not only focus on the development of business functions, but also unify the development style among different developers to a certain extent. In the process of using ABP framework, for the default project template generated by ABP, there are many class libraries and additional functions, which are the problems encountered in the process of implementing ABP within the Department. Therefore, this article will simplify the template project generated by ABP default, and build a simplified version of ABP project template

Template source code address:https://github.com/danvic712/ingos-abp-api-template

Step by Step

Before streamlining the project structure, we definitely need a default project template generated by ABP cli for our reference. There are two ways to create ABP projects

First, through the use ofABP CLITo create a basic project with such a dotnet tool, you only need to install ABP cli on the computer globally, and then you can generate the required project through scaffolding

--Global installation of ABP cli
dotnet tool install -g Volo.Abp.Cli

--Global update ABP cli
dotnet tool update -g Volo.Abp.Cli

When the installation is complete, enterabp helpYou can view the functions provided by the scaffold. For example, we need toabp newCommand to create a project. At the same time, based on different requirements, different parameters can be added when creating a project

ABP CLI

The second is directly through the official website(https://abp.io/get-started)Download, the project template used in this article is to use this method to adjust directly on the basis of the template downloaded from the official website

创建项目

When creating a project on the official website, you need to input the relevant information of the project. Because it is the web API project that needs to be modified, the UI framework selects angular. At the same time, because the default ABP template will mix the identityserver and API project, I will split the functions of these two blocks. After confirming the correct project type, click the create now button, Wait until the browser prompts you to download the file

2.1 operation template project

Unzip and download the completed installation package. If you have the same options as the project I created, there will be two folders here. Because only the back-end project is adjusted, only the content in the aspnet core folder can be focused here. Before the transformation, we first operate according to the instructions in the official documents to ensure that the template can run normally, and then make subsequent adjustments.

模板文件夹结构

2.1.1 adjust database connection string

In the template project generated by ABP, the configuration file appsettings.json of the whole project exists in three places,.DbMigrator.HttpApi.Host.IdentityServerInstead of paying attention to the specific functions of these three items, first modify the database connection strings in these three configuration files to the actual configuration information

"ConnectionStrings": {
    "Default": "Server=localhost;Port=3306;Database=IngosAbpTemplate;Uid=root;Pwd=myPassword;"
},

2.1.2. Perform database migration

After adjusting the database connection configuration, you can perform the database migration operation, so as to initialize some built-in data table structures or initialization data in the project template

Here, it will be.DbMigrator Set to start the project, run directly, wait for the program to complete. At this point, open the DBMS tool you are using, and you can see that the tables defined in the template project have been migrated to our specified database

执行数据库迁移

As you can see, the entire template project contains a lot of things, most of the functions are not usually used when we develop business functions

2.1.3 operation procedure

When the database is successfully migrated, it can be officially run. There are two web applications in this template program,.HttpApi.Host.IdentityServer, corresponding to the host of API interface and the host of identityserver respectively. Since the functions related to identityserver will be removed later, only the.HttpApi.HostThis is a site

take.HttpApi.HostSet to start the program, there is no debugging here, so run it directly through Ctrl + F5, and the project will automatically open the swagger page

Swagger

2.2. Simplify project functions

When the program can run, we can simplify and transform the template project, which mainly includes two parts, the function of simplifying the template and the structure of the project

2.2.1. Simplify template function

First of all, it will be removed here.IdentityServerAs you can see from the swagger page running above, this web project and the test folder that cannot be used at present contain some functions that may not be used in the development of business functions, and these functions are included in the ABP class library referenced by the project. Therefore, for the simplification of template function, some references that are not used will be usedVolo.Abp.*The class library is removed and only what we need is retained

Here, I remove the following assembly references and recompile the solution. Unexpectedly, many errors will be reported, because the specific debugging process will involve a lot of things, which is not easy to describe in words, so I skip it here

  • Volo.Abp.Account.*

  • Volo.Abp.Identity.*

  • Volo.Abp.IdentityServer.*

  • Volo.Abp.PermissionManagement.*

  • Volo.Abp.TenantManagement.*

  • Volo.Abp.FeatureManagement.*

  • Volo.Abp.SettingManagement.*

  • Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy

报错信息

The functions in the template project are basically removed, and only basic services such as audit log, background tasks, email notification, object mapping, and EF core are retained

To sum up, for compilation errors caused by removing the function, you only need to delete the relevant class files and references directly. For code problems caused by removing the function, you need to analyze them in detail. This kind of problem is basically the function of initializing data (dataseed), and I take the method of removing the related function directly

At this point, when you go to this step, you can also take advantage of the situation.DbMigratorThis console application specially used for database migration has been removed. For the migration function, I will add it to other class libraries in the following content

Oh, by the way, after removing the above functions, you need to add corresponding ABP assembly references to the following class libraries to ensure that the program can be compiled

  • .ApplicationquoteVolo.Abp.Ddd.Application
  • .Domain.SharedquoteVolo.Abp.Validation
  • .HttpApiquoteVolo.Abp.AspNetCore.Mvc
  • .HttpApi.ClientquoteVolo.Abp.Http.Client

If there is no accident, your application has been able to compile successfully and run again. As can be seen from swagger, most functions of the whole template have been removed. Of course, if you think this process is troublesome, you can also directly use the GitHub project listed at the beginning of the article, and then adjust it based on your needs

精简功能

2.3 simplify the project structure

Let’s go back to the original back-end template project. The global structure of the whole back-end solution project is as follows

解决方案结构

Because the class libraries related to unit testing have been removed, you can see from the project dependency diagram that the whole solution contains three top-level projects,.IdentityServer.HttpApi.Host.DbMigratorOther class libraries build the hierarchical foundation of the project through mutual reference

依赖关系

Of course, when streamlining the template function above, it has been implemented.IdentityServer.DbMigratorThis piece has been removed as a whole

2.3.1. Merge entityframework core related functional class libraries

Because the Entity Framework core (hereinafter referred to as EF core) is selected as the orm of the project, if code first mode is used, migration is inevitable. In the original template, there are three class libraries associated with it

  • . dbmigrator: console program, mainly for database migration
  • . entityframeworkcore: integrate EF core into the project, define dbcontext and Data Access Repository in the domain, and provide data persistence and data access in the whole project
  • . entityframeworkcore. Dbmigrations: performs EF core migration

As a modular framework, ABP defines the usage of each class library very clearly. However, in the actual development, the operation of the formal environment database is basically performed by the DBA, and the EF core migration is mostly used during development. At the same time, if this division is true, at least most of the developers I meet will call

Between the above has been.DbMigratorTherefore, all operations related to ef core are merged into.EntityFrameworkCoreIn this class library

For the sake of convenience, let’s.EntityFrameworkCore.DbMigrationsCopy all the files in to.EntityFrameworkCoreThis class library, and then directly remove the original class library in the solution before adjustment

PS: the migrations folder here and.EntityFrameworkCore.DbMigrationsAfter all, most of the tables no longer exist after we simplify the template function. After the class library is merged, the database migration will be performed again

合并 EF Core 相关类库

Because it’s going to be here now.EntityFrameworkCoreSo we need to add the official design package of EF core

Install-Package Microsoft.EntityFrameworkCore.Design

For from.DbMigrationsIn fact, there is a reference relationship in this class library. After the whole library is deleted, we need to.HttpApi.HostAdd for.EntityFrameworkCoreA reference to this class library. At the same time, for the copied files, there is a problem of duplicate functions, so here we need to merge and summarize these files

First, you need to merge the dbcontext object. As can be seen from the figure below, there are some differences between the two dbcontexts in the configuration of entity mapping relationship, so we will directlyMigrationsDbContextThe entity mapping configuration of ABP built-in framework in this class can be copied in the past, and then it can be deleted directly

合并 DbContext

For each ABP class library, there will be an entry module class. After comparison, you can find thatEntityFrameworkCoreDbMigrationsModuleThis module class can be deleted, and then you need to delete it.HttpApi.HostThe module type referenced in the project is changed toEntityFrameworkCoreModule

At this point, the problem of error reporting in the compilation solution is mainly due to the reference to the deleted dbcontext, which can be directly replaced by the current one

修复 DbContext 引用错误的问题

Here’s another thing to note. As you can see from line 27 in the figure above, what you get here is.DbMigratorThis is the configuration file in the console application, so it needs to be synchronously modified to get.HttpApi.HostProfile in project

Since then, the class libraries related to ef core functions have been merged and positioned to.EntityFrameworkCoreWith this class library, you can use the migrations command to perform database migration in the console. Because it has been executed once, you can delete the original library directly

--1. Create a migration file
dotnet ef migrations add Initialize

--2. Apply to database
dotnet ef database update

PS: you are using the dotnet tool EF core tools. If the console prompts you that you can’t find the command, you need to install the tool on your computer first

dotnet tool install --global dotnet-ef

Wait for the migration work to complete, the database also regenerates the corresponding table, and then run the project again. After the system runs correctly, the subsequent operation can be carried out

迁移完成

2.3.2 merge API host related functions

Generally speaking, we will define the controller in the asp.net core project and use it as the host program of the whole interface project. In ABP, the API interface includes the following three class libraries

  • . httpapi: defines the controller of the API interface
  • . httpapi. Client: used to set the HTTP proxy for other C # programs to call the API
  • . httpapi. Host: the host program of API interface

there.HttpApi.ClientWe can’t use the class library of, so we can delete it directly. The remaining two class libraries will be merged and unified to.HttpApi.HostIn this asp.net core project

For the combination of the two libraries, it is very simple to directly merge the two libraries.HttpApiThe class files in the project are copied directly to the.HttpApi.HostThen move the configuration in the module class and delete it directly.HttpApiClass library, and the relevant reference can be deleted

合并 API 相关功能

At this point, the simplification of the whole template project is over. Based on the current development situation, a relatively single template project is constructed with the design idea of ABP retained. The adjusted project reference diagram is as follows

项目引用关系图

2.4 construction of project scaffold

When the whole adjustment is completed, it is impossible to perform this operation again when creating a new project. Therefore, a nuget package will be created for publishing, so that the project can be created directly through the dotnet cli command

For the process of building a project template here is not described in detail, if you need students, you can refer to this article in the blog Garden(Create your own. Net core project template)In the process, we need to create two new filestemplate.jsonas well asIngos.Abp.Templates.csprojThe adjusted project folder structure is as follows

项目脚手架结构

Here is a list of my current configurations. You can refer to them. If you need to add other parameters, you can refer to the official website documents(Dotnet new custom template

{
  "$schema": "http://json.schemastore.org/template",
  "author": "Danvic Wang",
  "classifications": [ "Web/WebAPI" ],
  "name": "Ingos Web API with ABP Framework",
  "identity": "Ingos.Abp.Templates",
  "shortName": "ingos-abp",
  "tags": {
    "language": "C#",
    "type": "project"
  },
  "sourceName": "IngosAbpTemplate",
  "preferNameDirectory": true
}
Template
    1.0.1
    Ingos.Abp.Templates
    Ingos.Abp.Templates
    Danvic Wang
    Ingos Web API with ABP Framework
    dotnet-new;templates;abp;domain-driven-design

    net5.0

    true
    false
    content
    $(NoWarn);NU5128
    MIT
    Copyright (c) 2021 Danvic Wang
    logo.png
    Ingos.Abp.Templates
    Danvic Wang
    https://github.com/danvic712/ingos-abp-api-template
    https://github.com/danvic712/ingos-abp-api-template
    git
  

  
    
    
  

  
    
      True

When the configuration definition is complete, navigate toIngos.Abp.Templates.csprojPath, executiondotnet pack -o .Command to generate a.nupkgAfter that, the file can be uploaded to the public or private nuget warehouse for others to download

Take the project I created here as an example. Since I have uploaded it to the official nuget repository, you can use the following command to install it

dotnet new -i Ingos.Abp.Templates

安装项目模板

After the installation, you can create the project through the dotnet command. In the latest version of VS, you can also use the template to create the project directly through IDE, which is relatively more convenient. At this point, the simplified operation of the whole template is over. I hope it can help you

通过 VS 创建项目