在上一篇文章《手把手教你用Abp vnext構建API接口服務》中,咱們用ABP vnext實現了WebAPI接口服務,可是並不是ABP模塊化架構的最佳實踐。我自己也在學習ABP,我認爲ABP新手應該從最佳實踐開始學習,能夠少走不少彎路,因此寫了這篇最佳實踐來作個總結,同時給其餘朋友們提供一點參考。html
ABP是一個包含許多nuget包的模塊化框架。它提供了一個完整的基礎架構來開發你本身的具備實體、服務、數據庫集成、API、 UI組件等等功能的應用程序模塊。
模塊化架構的最佳實踐就是按功能的劃分建立相應的模塊實現的服務,必要時能夠把模塊發佈到nuget,由其餘模塊安裝組合成新的服務或單體應用。這樣就能夠複用現有的模塊功能,快速的交付產品,告別996。mysql
這部份內容所有來自於模塊化架構最佳實踐 & 約定,有興趣的朋友能夠去官方文檔查看。若是已經很熟悉ABP分層了,能夠直接閱讀第三部分。git
將領域層劃分爲兩個項目:github
將應用服務層劃分爲兩個項目:web
建立命名爲CompanyName.ModuleName.Web的Web包。 包含頁面、視圖、腳本、樣式、圖像和其餘UI組件。Web包僅依賴於HttpApi包。sql
開發環境:Mac Visual Studio Code
SDK:dotnet core 3.1
數據庫:PostgreSQL數據庫
在上一篇文章中引入的是Volo.Abp.Identity的包,如今改用Volo.Abp.DDD的包,更簡潔。api
/**建立解決方案**/ dotnet new sln /**建立模塊分層**/ dotnet new classlib -o src/Lemon.Account.Domain.Shared dotnet new classlib -o src/Lemon.Account.Domain dotnet new classlib -o src/Lemon.Account.EntityFrameworkCore dotnet new classlib -o src/Lemon.Account.EntityFrameworkCore.DbMigrations dotnet new classlib -o src/Lemon.Account.Application.Contracts dotnet new classlib -o src/Lemon.Account.Application dotnet new classlib -o src/Lemon.Account.HttpApi dotnet new classlib -o src/Lemon.Account.HttpApi.Client dotnet new web -o src/Lemon.Account.Host /**加入解決方案**/ dotnet sln Lemon.Account.sln add src/Lemon.Account.Domain.Shared dotnet sln Lemon.Account.sln add src/Lemon.Account.Domain dotnet sln Lemon.Account.sln add src/Lemon.Account.EntityFrameworkCore dotnet sln Lemon.Account.sln add src/Lemon.Account.EntityFrameworkCore.DbMigrations dotnet sln Lemon.Account.sln add src/Lemon.Account.Application.Contracts dotnet sln Lemon.Account.sln add src/Lemon.Account.Application dotnet sln Lemon.Account.sln add src/Lemon.Account.HttpApi dotnet sln Lemon.Account.sln add src/Lemon.Account.HttpApi.Client dotnet sln Lemon.Account.sln add src/Lemon.Account.Host /**分層間互相引用**/ dotnet add src/Lemon.Account.Domain/Lemon.Account.Domain.csproj reference src/Lemon.Account.Domain.Shared/Lemon.Account.Domain.Shared.csproj dotnet add src/Lemon.Account.EntityFrameworkCore/Lemon.Account.EntityFrameworkCore.csproj reference src/Lemon.Account.Domain/Lemon.Account.Domain.csproj dotnet add src/Lemon.Account.EntityFrameworkCore.DbMigrations/Lemon.Account.EntityFrameworkCore.DbMigrations.csproj reference src/Lemon.Account.EntityFrameworkCore/Lemon.Account.EntityFrameworkCore.csproj dotnet add src/Lemon.Account.Application/Lemon.Account.Application.csproj reference src/Lemon.Account.Application.Contracts/Lemon.Account.Application.Contracts.csproj dotnet add src/Lemon.Account.Application/Lemon.Account.Application.csproj reference src/Lemon.Account.Domain/Lemon.Account.Domain.csproj dotnet add src/Lemon.Account.HttpApi/Lemon.Account.HttpApi.csproj reference src/Lemon.Account.Application.Contracts/Lemon.Account.Application.Contracts.csproj dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj reference src/Lemon.Account.EntityFrameworkCore.DbMigrations/Lemon.Account.EntityFrameworkCore.DbMigrations.csproj dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj reference src/Lemon.Account.Application/Lemon.Account.Application.csproj dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj reference src/Lemon.Account.HttpApi/Lemon.Account.HttpApi.csproj /**添加nuget依賴包**/ dotnet add src/Lemon.Account.Domain.Shared/Lemon.Account.Domain.Shared.csproj package Volo.Abp.Validation dotnet add src/Lemon.Account.Domain/Lemon.Account.Domain.csproj package Volo.Abp.Ddd.Domain dotnet add src/Lemon.Account.Domain/Lemon.Account.Domain.csproj package Lemon.Common dotnet add src/Lemon.Account.EntityFrameworkCore/Lemon.Account.EntityFrameworkCore.csproj package Volo.Abp.EntityFrameworkCore dotnet add src/Lemon.Account.EntityFrameworkCore/Lemon.Account.EntityFrameworkCore.csproj package Volo.Abp.EntityFrameworkCore.PostgreSql dotnet add src/Lemon.Account.EntityFrameworkCore.DbMigrations/Lemon.Account.EntityFrameworkCore.DbMigrations.csproj package Volo.Abp.EntityFrameworkCore dotnet add src/Lemon.Account.EntityFrameworkCore.DbMigrations/Lemon.Account.EntityFrameworkCore.DbMigrations.csproj package Volo.Abp.EntityFrameworkCore.PostgreSql dotnet add src/Lemon.Account.EntityFrameworkCore.DbMigrations/Lemon.Account.EntityFrameworkCore.DbMigrations.csproj package Microsoft.EntityFrameworkCore.Design dotnet add src/Lemon.Account.Application.Contracts/Lemon.Account.Application.Contracts.csproj package IdentityModel dotnet add src/Lemon.Account.Application.Contracts/Lemon.Account.Application.Contracts.csproj package Volo.Abp.Ddd.Application.Contracts dotnet add src/Lemon.Account.Application/Lemon.Account.Application.csproj package Volo.Abp.Ddd.Application dotnet add src/Lemon.Account.Application/Lemon.Account.Application.csproj package Volo.Abp.AutoMapper dotnet add src/Lemon.Account.HttpApi/Lemon.Account.HttpApi.csproj package Volo.Abp.AspNetCore.Mvc dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Volo.Abp.AspNetCore.Mvc dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Swashbuckle.AspNetCore dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Microsoft.AspNetCore.DataProtection.StackExchangeRedis dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Microsoft.Extensions.Caching.StackExchangeRedis dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Serilog.AspNetCore dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Serilog.Settings.Configuration dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Serilog.Sinks.Async dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Serilog.Sinks.File dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Volo.Abp.AspNetCore.Serilog dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package Volo.Abp.Autofac dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj package IdentityServer4.AccessTokenValidation dotnet add src/Lemon.Account.Host/Lemon.Account.Host.csproj reference src/Lemon.Account.EntityFrameworkCore.DbMigrations/Lemon.Account.EntityFrameworkCore.DbMigrations.csproj
在Domain建立實體類及其約束關聯,具體代碼請看UserData類,此處再也不詳細說明。瀏覽器
在EntityFrameworkCore建立AccountDbContext類和LemonAccountDbContextModelCreatingExtensions類。請特別關注LemonAccountDbContextModelCreatingExtensions類,主要是用來定義實體在數據庫表中的字段屬性。安全
進入Host目錄,執行命令
dotnet watch run
服務啓動成功,在瀏覽器打開連接http://localhost:5000/swagger/index.html,開始調試帳號註冊和驗證接口。結果以下:
使用模塊化架構的好處有幾點:
dotnet new classlib -o src/Lemon.Account.EntityFrameworkCore.MySQL dotnet new classlib -o src/Lemon.Account.EntityFrameworkCore.MySQL.DbMigrations dotnet sln Lemon.Account.sln add src/Lemon.Account.EntityFrameworkCore.MySQL dotnet sln Lemon.Account.sln add src/Lemon.Account.EntityFrameworkCore.MySQL.DbMigrations dotnet add src/Lemon.Account.EntityFrameworkCore.MySQL/Lemon.Account.EntityFrameworkCore.MySQL.csproj reference src/Lemon.Account.Domain/Lemon.Account.Domain.csproj dotnet add src/Lemon.Account.EntityFrameworkCore.MySQL.DbMigrations/Lemon.Account.EntityFrameworkCore.MySQL.DbMigrations.csproj reference src/Lemon.Account.EntityFrameworkCore.MySQL/Lemon.Account.EntityFrameworkCore.MySQL.csproj dotnet add src/Lemon.Account.EntityFrameworkCore.MySQL/Lemon.Account.EntityFrameworkCore.MySQL.csproj package Volo.Abp.EntityFrameworkCore.MySQL dotnet add src/Lemon.Account.EntityFrameworkCore.MySQL.DbMigrations/Lemon.Account.EntityFrameworkCore.MySQL.DbMigrations.csproj package Microsoft.EntityFrameworkCore.Design
最後再修改ConnectionStrings default爲mysql的鏈接,修改LemonAccountHostModule的依賴項LemonAccountEntityFrameworkCoreDbMigrationsModule爲MySQL
單體應用和微服務無縫切換
模塊開發完成後,通常看成微服務來部署;或者把模塊發佈到nuget,在單體應用的各個分層中引入模塊,多個模塊在單體應用中的應用就構成了一個完整系統服務。
模塊複用
因爲模塊是發佈到nuget的,因此若是有新的項目須要用到相同功能的,能夠直接從nuget引入安裝,無需重複開發或者複製代碼。
自動API控制器
動態 C# API 客戶端
GiHub地址:https://github.com/huangbenq/abp-samples
參考文檔: