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 styles among different developers to a certain extent. In the process of using ABP framework, the default project template generated by ABP has many class libraries and additional functions, which are the problems encountered in the implementation of ABP within the department. Therefore, this article will simplify the template project generated by ABP by default and build a simplified version of the ABP project template
Template source address: github.com/danvic712/i…
Step by Step
Before streamlining the project structure, we definitely need a default project template generated by the ABP CLI for reference. There are two main ways to create an ABP project
The first is to create a base project using a dotnet tool called ABP CLI that scaffolding the project by installing ABP CLI globally on your computer
CLI -- Global update ABP CLI dotnet tool update -g volo.abp.cliCopy the code
When the installation is complete, type ABP help to see what the scaffold provides. For example, here we need to create the project with the abP new command, and attach different parameters to the project based on different requirements
The second method is to download directly from the official website (abp.io/get-started). The project template used in this article is adjusted based on the template downloaded directly from the official website
The UI framework is Angular, and the default ABP template mixes IdentityServer with the API project. Therefore, I will split the functions of these two pieces. After confirming that the project type is correct, click the Create now button and wait for the browser to prompt that there is a file to download
Run the template project
Unzip the downloaded installation package, and if you and I have created the same project options, there will be two folders, because the adjustment is only for the back-end project, only the aspnet-core folder will be the focus. Before the transformation, we first follow the instructions in the official documents to make sure that the template can run normally and then make subsequent adjustments.
Adjust the database connection string
In the template project generated by ABP, the configuration file appsettings.json exists in three places:.dbmigrator,.httpapi.Host and.identityServer. Start by modifying the database connection strings in these three configuration files to the actual configuration information used
"ConnectionStrings": {
"Default": "Server=localhost; Port=3306; Database=IngosAbpTemplate; Uid=root; Pwd=myPassword;"
},
Copy the code
Perform database migration
After adjusting the database connection configuration, you can perform database migration operations to initialize some of the data table structures built into the project template or initialize data
Here, set.dbmigrator to the start project and run it directly, waiting for the program to complete. At this point, open the DBMS tool you are using and see that the tables defined in the template project have been migrated to the database we specified
As you can see, there is a lot going on in the template project, and most of the functionality is not something we would normally use to develop business functionality
To run the program
HttpApi.Host and.IdentityServer, which correspond to the Host of the API interface and the Host of the IdentityServer, respectively. Since the identityServer-related functionality will be removed later, only.httpapi.Host will be used
Set.httpapi.Host as the launcher. Debugging is not done here, so run it directly with Ctrl + F5 and the project will automatically open the Swagger page
Streamlining project functions
When the program can run, you can simplify and transform the template project, which mainly contains two contents, simplify the function of the template and simplify the structure of the project
Thin Provisioning templates
The first step is to remove the.IdentityServer Web project and the currently unused test folder. As you can see from the Swagger page running above, the initialized template contains some functionality that may not be needed for business functionality development. These capabilities are included in the ABP library referenced by the project. Therefore, the simplification of template functionality is to remove some of the unused volo.abp.* libraries referenced, leaving only the parts we need
Here I remove the following assembly references and recompile the solution. Unsurprisingly, many errors will be reported, because the specific troubleshooting process involves too much 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
Here, 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
In summary, to cause a compiler error after removing function, only need the related class files, references, delete directly, and to the problem of function after removing the generated code will need to be analyzed, this kind of problem basically is initialized data (DataSeed) function, my side is taken directly to remove the related functions
At this point, when you get to this point, you can also remove the.dbmigrator console application that is specifically used for database migration, which I will add to other libraries in the following sections
By the way, after removing the above functionality, you also need to add the corresponding ABP assembly reference to the following libraries to ensure that the program will compile
.Application
referenceVolo.Abp.Ddd.Application
.Domain.Shared
referenceVolo.Abp.Validation
.HttpApi
referenceVolo.Abp.AspNetCore.Mvc
.HttpApi.Client
referenceVolo.Abp.Http.Client
If nothing else, your application will compile and run again, with Swagger removing most of the functionality from the template. Of course, if you find this a bit of a hassle, you can just use the Github project listed at the beginning of this article and tweak it to suit your needs
Simplify the project structure
Back to the original back-end template project, the overall project global structure of the back-end solution is shown below
Since the unit test libraries are removed, the project dependency diagram shows that the solution consists of the top three projects,.identityServer,.httpapi.Host, and.dbMigrator. The other libraries reference each other to build a layered foundation for the project
Of course, the.IdentityServer and.DbMigrator sections were removed from the template simplification above
Merge EntityFramework Core libraries
Since EntityFramework Core (EF Core for short) was selected as the ORM of the project, it was inevitable that migrations would be used if Code First was used. In the original template, There are three libraries associated with it as follows
- .dbmigrator: console application, mainly for database migration.
- EntityFrameworkCore: Integrates EF Core into a project, defines DbContext and a Repository of data access in the domain, and provides data persistence and data access throughout the project
- . EntityFrameworkCore. DbMigrations: perform EF Core of migration
It can be seen that ABP, as a modular framework, has a very clear definition of the use of each class library. However, in actual development, the operation of the formal environment database is basically entrusted to the DBA, and the migration of EF Core is mostly used during development. At the same time, at least the vast majority of developers I’ve met would have said yes if that were the case
Since.dbmigrator has been removed above, all operations on EF Core are merged into the.EntityFrameworkCore library
For convenience, we first. EntityFrameworkCore. DbMigrations copy all the files from a to the EntityFrameworkCore this library, and then directly in the solution to remove the original class library and then to adjust
PS: Here the Migrations of folders and. EntityFrameworkCore. DbMigrations module class can directly deleted, after all the above we can simplify the function of template, the vast majority of table also ceased to exist, behind in the class library will be combined to perform the operation of the database migration
Since migration is now performed on the.EntityFrameworkCore, the official EF Core Design package needs to be added here
Install-Package Microsoft.EntityFrameworkCore.Design
Copy the code
For.dbmigrations, there are references to the.entityFrameworkCore library on.httpapi.Host. At the same time, for the copied files, there is a problem of function duplication, so here we need to merge these files
First, you need to merge the DbContext database context object. As you can see from the following figure, the two dbContexts differ mainly in the configuration of the entity mapping relationship, so you can simply copy the entity mapping configuration of the ABP built-in framework in MigrationsDbContext and delete it
For each ABP class library, there are an entry module class, after the contrast can be found here, directly to the EntityFrameworkCoreDbMigrationsModule deletes the module class, Then you need to. HttpApi. Host projects refer to the module type EntityFrameworkCoreModule instead
In this case, when compiling the solution, the error is mainly due to the reference to the deleted DbContext, which can be replaced by the current DbContext
One more thing to note here is that line 27 in the figure above shows that the configuration file is retrieved from the.dbmigrator console application, so it also needs to be synchronized to the configuration file from the.httpapi.host project
Since then, libraries related to EF Core functions have been merged, and migrations can be performed from the console by using the.EntityFrameworkCore migrations command. Since this has already been done once, you can simply delete the original library
Dotnet EF migrations add Initialize dotnet EF Database UpdateCopy the code
PS: The EF Core Tools dotnet tool is used here. If the console tells you that you can’t find the command, you need to install this tool on your computer
dotnet tool install --global dotnet-ef
Copy the code
Wait for the completion of the migration, the database has also re-generated the corresponding table, at this time run the project again, after the system runs correctly, the subsequent operations can be carried out
Merge API host-related functions
Generally speaking, we define controllers in ASP.NET Core projects and use them as hosts for the entire interface project. In ABP, the apis involved include the following three libraries
- .httpapi: defines controller of API interface
- .httpapi. Client: HTTP proxy used to set up other C# programs to call this API
- .httpapi.Host: Host program for the API interface
The.httpapi.Client library is not available, so you can delete it directly. The remaining two libraries will be combined into.httpapi
It is easy to merge the two libraries: copy the.httpapi project class files directly to.httpapi.Host, then move the configuration of the module class, and finally delete the.httpapi library and the related references
At this point, the simplification of the whole template project ended. A relatively single template project was constructed based on the development status while retaining the design idea of ABP. The adjusted project reference diagram is shown below
Building project scaffolding
After the adjustment is complete, it is not possible to do this again when creating a new project, so a Nuget package will be created and distributed so that the project can be created directly using dotnet CLI commands
The process of building a project template is not described in detail here. If you need it, you can refer to this article in the blog garden. NET Core project template), in the process, we need to create two files template. Json and Ingos. Abp. Templates. The csproj, adjusted the project folder structure as shown below
The configuration I’m currently using is listed here for your reference, or if you need to add additional parameters, you can refer to the dotnet New custom template documentation.
{
"$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
}
Copy the code
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageType>Template</PackageType>
<PackageVersion>1.0.1</PackageVersion>
<PackageId>Ingos.Abp.Templates</PackageId>
<Title>Ingos.Abp.Templates</Title>
<Authors>Danvic Wang</Authors>
<Description>Ingos Web API with ABP Framework</Description>
<PackageTags>dotnet-new; templates; abp; domain-driven-design</PackageTags>
<TargetFramework>net5.0</TargetFramework>
<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>
<NoWarn>$(NoWarn); NU5128</NoWarn>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright (c) 2021 Danvic Wang</Copyright>
<PackageIcon>logo.png</PackageIcon>
<Product>Ingos.Abp.Templates</Product>
<Company>Danvic Wang</Company>
<RepositoryUrl>https://github.com/danvic712/ingos-abp-api-template</RepositoryUrl>
<PackageProjectUrl>https://github.com/danvic712/ingos-abp-api-template</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<ItemGroup>
<Content Include="templates\**\*" Exclude="templates\.vs\**; templates\**\Logs\**; templates\**\bin\**; templates\**\obj\**;" />
<Compile Remove="\ * * *" />
</ItemGroup>
<ItemGroup>
<None Include="resource\images\logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
Copy the code
After the configuration is defined, locating the Ingos. Abp. Templates. The csproj path, perform dotnet pack – o. The command generates an.nupkg file with a version number, which can then be uploaded to a public or private Nuget repository 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 install it using the following command
dotnet new -i Ingos.Abp.Templates
Copy the code
Once installed, you can use dotnet commands to create projects. In the latest version of VS, you can also use the template directly from the IDE to create projects, which is relatively convenient. At this point, the whole template streamlined operation is finished, hope you can help
Signature
Author: Mo Mo Mo Mo Xiaoyu
Biography: Born in 1996, born in a fourth-tier city in Anhui province, graduated from Top 10 million universities. .NET programmer, gunslinger, cat. It will begin in December 2016. NET programmer career, Microsoft. NET technology stalwart, aspired to be the cloud cat kid programming for Google the best. NET programmers
Personal blog: Yuiter.com
Garden blog blog: www.cnblogs.com/danvic712