ABP是一个开源应用程序框架,该项目是ASP.NET Boilerplate Web应用程序框架的下一代,专注于基于ASP.NET Core的Web应用程序开发,也支持开发控制台应用程序。

官方网站:https://abp.io/
官方文档:https://docs.abp.io/

ABP CLI是使用ABP框架启动新解决方案的最快方法。如果没有安装ABP CLI,使用命令行窗口安装ABP CLI:

  1. dotnet tool install -g Volo.Abp.Cli
  1. abp new Acme.BookStore

您可以使用不同级别的名称空间。例如BookStore,Acme.BookStore或Acme.Retail.BookStore。

这样,就已经完成了一个应用程序的搭建。

然后我们只需要修改一下其他的配置即可运行应用程序,开发人员在这个架构的基础上就可以愉快的撸代码了。

然而,ABP的学习才刚刚开始。ABP放弃了原有MVC的架构,使用了模块化架构,支持微服务,根据DDD模式和原则设计和开发,为应用程序提供分层模型。对于没有微服务开发经验的程序员来说,学习ABP难度比较大。下面我们开始从一个空的web解决方案,一步步搭建API接口服务。

开发环境:Mac Visual Studio Code
SDK:dotnet core 3.1

使用命令创建一个空的web方案:

  1. dotnet new web -o Lemon.UserCenter.HttpApi.Hosting
  1. 创建api
  2. dotnet new classlib -o Lemon.UserCenter.HttpApi
  3. 创建应用层
  4. dotnet new classlib -o Lemon.UserCenter.Application
  5. 创建领域层
  6. dotnet new classlib -o Lemon.UserCenter.Domain
  7. 创建基于EntityFrameworkCore的数据层
  8. dotnet new classlib -o Lemon.UserCenter.EntityFrameworkCore
  1. 创建解决方案
  2. dotnet new sln
  3. 所有类库加入解决方案
  4. dotnet sln Lemon.UserCenter.sln add Lemon.UserCenter.HttpApi.Hosting/Lemon.UserCenter.HttpApi.Hosting.csproj
  5. dotnet sln Lemon.UserCenter.sln add Lemon.UserCenter.HttpApi/Lemon.UserCenter.HttpApi.csproj
  6. dotnet sln Lemon.UserCenter.sln add Lemon.UserCenter.Application/Lemon.UserCenter.Application.csproj
  7. dotnet sln Lemon.UserCenter.sln add Lemon.UserCenter.Domain/Lemon.UserCenter.Domain.csproj
  8. dotnet sln Lemon.UserCenter.sln add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj
  9. 添加项目引用
  10. dotnet add Lemon.UserCenter.HttpApi.Hosting/Lemon.UserCenter.HttpApi.Hosting.csproj reference Lemon.UserCenter.HttpApi/Lemon.UserCenter.HttpApi.csproj
  11. dotnet add Lemon.UserCenter.HttpApi.Hosting/Lemon.UserCenter.HttpApi.Hosting.csproj reference Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj
  12. dotnet add Lemon.UserCenter.HttpApi/Lemon.UserCenter.HttpApi.csproj reference Lemon.UserCenter.Application/Lemon.UserCenter.Application.csproj
  13. dotnet add Lemon.UserCenter.Application/Lemon.UserCenter.Application.csproj reference Lemon.UserCenter.Domain/Lemon.UserCenter.Domain.csproj
  14. dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj reference Lemon.UserCenter.Domain/Lemon.UserCenter.Domain.csproj

领域层添加Volo.Abp.Identity.Domain包引用:

  1. dotnet add Lemon.UserCenter.Domain/Lemon.UserCenter.Domain.csproj package Volo.Abp.Identity.Domain

创建领域层模块类:

  1. using Volo.Abp.Identity;
  2. using Volo.Abp.Modularity;
  3. namespace Lemon.UserCenter.Domain
  4. {
  5. [DependsOn(typeof(AbpIdentityDomainModule))]
  6. public class UserCenterDomainModule : AbpModule
  7. {
  8. }
  9. }

创建实体类:

  1. using System;
  2. using Volo.Abp.Domain.Entities;
  3. namespace Lemon.UserCenter.Domain
  4. {
  5. public class UserData : Entity<Guid>
  6. {
  7. /// <summary>
  8. /// 账号
  9. /// </summary>
  10. /// <value>The account.</value>
  11. public string Account { get; set; }
  12. /// <summary>
  13. /// 昵称
  14. /// </summary>
  15. /// <value>The name of the nike.</value>
  16. public string NickName { get; set; } = "";
  17. /// <summary>
  18. /// 头像
  19. /// </summary>
  20. /// <value>The head icon.</value>
  21. public string HeadIcon { get; set; } = "";
  22. /// <summary>
  23. /// 手机号码
  24. /// </summary>
  25. /// <value>The mobile.</value>
  26. public string Mobile { get; set; } = "";
  27. /// <summary>
  28. /// 电子邮箱
  29. /// </summary>
  30. /// <value>The email.</value>
  31. public string Email { get; set; } = "";
  32. /// <summary>
  33. /// 删除注记
  34. /// </summary>
  35. /// <value><c>true</c> if deleted; otherwise, <c>false</c>.</value>
  36. public bool Deleted { get; set; }
  37. }
  38. }

数据层添加引用:

  1. dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Volo.Abp.EntityFrameworkCore
  2. dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Volo.Abp.EntityFrameworkCore.PostgreSQL
  3. dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Microsoft.EntityFrameworkCore.Design
  4. dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Microsoft.EntityFrameworkCore
  5. dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Microsoft.EntityFrameworkCore.Relational

在这里我们使用的是PostgreSQL数据库,所以引用了Volo.Abp.EntityFrameworkCore.PostgreSQL,如果使用的是MySQL,就要引用Volo.Abp.EntityFrameworkCore.MySQL,如果使用的是sqlserver,就要引用Volo.Abp.EntityFrameworkCore.SQLServer。

加入UserCenterDbContext类:

  1. using Lemon.UserCenter.Domain;
  2. using Microsoft.EntityFrameworkCore;
  3. using Volo.Abp.Data;
  4. using Volo.Abp.EntityFrameworkCore;
  5. namespace Lemon.UserCenter.EntityFrameworkCore
  6. {
  7. [ConnectionStringName("Default")]
  8. public class UserCenterDbContext : AbpDbContext<UserCenterDbContext>
  9. {
  10. public DbSet<UserData> UserData { get; set; }
  11. public UserCenterDbContext(DbContextOptions<UserCenterDbContext> options)
  12. : base(options)
  13. {
  14. }
  15. protected override void OnModelCreating(ModelBuilder builder)
  16. {
  17. base.OnModelCreating(builder);
  18. }
  19. }
  20. }

加入UserCenterDbContextFactory类:

  1. using System.IO;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.EntityFrameworkCore.Design;
  4. using Microsoft.Extensions.Configuration;
  5. namespace Lemon.UserCenter.EntityFrameworkCore
  6. {
  7. public class UserCenterDbContextFactory: IDesignTimeDbContextFactory<UserCenterDbContext>
  8. {
  9. public UserCenterDbContext CreateDbContext(string[] args)
  10. {
  11. var configuration = BuildConfiguration();
  12. var builder = new DbContextOptionsBuilder<UserCenterDbContext>()
  13. .UseNpgsql(configuration.GetConnectionString("Default"));
  14. return new UserCenterDbContext(builder.Options);
  15. }
  16. private static IConfigurationRoot BuildConfiguration()
  17. {
  18. var builder = new ConfigurationBuilder()
  19. .SetBasePath(Directory.GetCurrentDirectory())
  20. .AddJsonFile("appsettings.json", optional: false);
  21. return builder.Build();
  22. }
  23. }
  24. }

加入appsettings.json配置,用于生成数据迁移代码:

  1. {
  2. "ConnectionStrings": {
  3. "Default": "server=127.0.0.1;port=5432;Database=abp-samples-user-center;uid=postgres;pwd=123456"
  4. }
  5. }

创建数据层模块类:

  1. using Lemon.UserCenter.Domain;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Volo.Abp.EntityFrameworkCore;
  5. using Volo.Abp.EntityFrameworkCore.PostgreSql;
  6. using Volo.Abp.Modularity;
  7. namespace Lemon.UserCenter.EntityFrameworkCore
  8. {
  9. [DependsOn(typeof(UserCenterDomainModule),
  10. typeof(AbpEntityFrameworkCoreModule),
  11. typeof(AbpEntityFrameworkCorePostgreSqlModule))]
  12. public class UserCenterentityFrameworkCoreModule : AbpModule
  13. {
  14. public override void ConfigureServices(ServiceConfigurationContext context)
  15. {
  16. context.Services.AddAbpDbContext<UserCenterDbContext>(options => {
  17. options.AddDefaultRepositories(includeAllEntities: true);
  18. });
  19. Configure<AbpDbContextOptions>(options =>
  20. {
  21. options.Configure(ctx =>
  22. {
  23. if (ctx.ExistingConnection != null)
  24. {
  25. ctx.DbContextOptions.UseNpgsql(ctx.ExistingConnection);
  26. }
  27. else
  28. {
  29. ctx.DbContextOptions.UseNpgsql(ctx.ConnectionString);
  30. }
  31. });
  32. });
  33. #region 自动迁移数据库
  34. context.Services.BuildServiceProvider().GetService<UserCenterDbContext>().Database.Migrate();
  35. #endregion 自动迁移数据库
  36. }
  37. }
  38. }

生成数据迁移代码:

  1. dotnet ef migrations add InitialCreate --project Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj

在数据层下生成一个Migrations文件夹,里面的代码就是数据迁移代码,执行以下命令即可在数据库中自动生成数据库表:

  1. dotnet ef database update --project Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj

应用层添加Volo.Abp.Identity.Application应用:

  1. dotnet add Lemon.UserCenter.Application/Lemon.UserCenter.Application.csproj package Volo.Abp.Identity.Application

创建应用层模块类:

  1. using Volo.Abp.Modularity;
  2. using Volo.Abp.Identity;
  3. namespace Lemon.UserCenter.Application
  4. {
  5. [DependsOn(typeof(AbpIdentityApplicationModule))]
  6. public class UserCenterApplicationModule : AbpModule
  7. {
  8. }
  9. }

创建用户接口:

  1. using System.Threading.Tasks;
  2. using Lemon.UserCenter.Domain;
  3. namespace Lemon.UserCenter.Application
  4. {
  5. public interface IUserService
  6. {
  7. Task<UserData> Create(UserData data);
  8. }
  9. }

实现用户服务:

  1. using System;
  2. using System.Threading.Tasks;
  3. using Lemon.UserCenter.Domain;
  4. using Volo.Abp.Application.Services;
  5. using Volo.Abp.Domain.Repositories;
  6. namespace Lemon.UserCenter.Application
  7. {
  8. public class UserService : ApplicationService, IUserService
  9. {
  10. private readonly IRepository<UserData, Guid> _repository;
  11. public UserService(IRepository<UserData, Guid> repository)
  12. {
  13. this._repository = repository;
  14. }
  15. public async Task<UserData> Create(UserData data)
  16. {
  17. return await _repository.InsertAsync(data);
  18. }
  19. }
  20. }

api层添加Volo.Abp.Identity.HttpApi引用:

  1. dotnet add Lemon.UserCenter.HttpApi/Lemon.UserCenter.HttpApi.csproj package Volo.Abp.Identity.HttpApi

创建模块类:

  1. using Lemon.UserCenter.Application;
  2. using Volo.Abp.Identity;
  3. using Volo.Abp.Modularity;
  4. namespace Lemon.UserCenter.HttpApi
  5. {
  6. [DependsOn(typeof(AbpIdentityHttpApiModule),
  7. typeof(UserCenterApplicationModule))]
  8. public class UserCenterHttpApiModule : AbpModule
  9. {
  10. }
  11. }

创建controller:

  1. using System.Threading.Tasks;
  2. using Lemon.UserCenter.Application;
  3. using Lemon.UserCenter.Domain;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Volo.Abp.AspNetCore.Mvc;
  6. namespace Lemon.UserCenter.HttpApi.Controllers
  7. {
  8. [Route("api/user")]
  9. public class UserController : AbpController
  10. {
  11. private readonly IUserService _userService;
  12. public UserController(IUserService userService)
  13. {
  14. this._userService = userService;
  15. }
  16. [HttpPost("create")]
  17. public async Task<IActionResult> Create(UserData data)
  18. {
  19. var result = await _userService.Create(data);
  20. return Json(result);
  21. }
  22. }
  23. }

添加Volo.Abp.Autofac引用:

  1. dotnet add Lemon.UserCenter.HttpApi.Hosting/Lemon.UserCenter.HttpApi.Hosting.csproj package Volo.Abp.Autofac

创建模块类

  1. using Lemon.UserCenter.Domain;
  2. using Lemon.UserCenter.EntityFrameworkCore;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.Extensions.Hosting;
  5. using Volo.Abp;
  6. using Volo.Abp.AspNetCore.Mvc;
  7. using Volo.Abp.Autofac;
  8. using Volo.Abp.Modularity;
  9. namespace Lemon.UserCenter.HttpApi.Hosting
  10. {
  11. [DependsOn(typeof(UserCenterHttpApiModule),
  12. typeof(UserCenterDomainModule),
  13. typeof(UserCenterentityFrameworkCoreModule),
  14. typeof(AbpAspNetCoreMvcModule),
  15. typeof(AbpAutofacModule))]
  16. public class UserCenterHttpApiHostingModule: AbpModule
  17. {
  18. public override void OnApplicationInitialization(
  19. ApplicationInitializationContext context)
  20. {
  21. var app = context.GetApplicationBuilder();
  22. var env = context.GetEnvironment();
  23. if (env.IsDevelopment())
  24. {
  25. app.UseDeveloperExceptionPage();
  26. }
  27. else
  28. {
  29. app.UseExceptionHandler("/Error");
  30. }
  31. app.UseStaticFiles();
  32. app.UseRouting();
  33. app.UseMvcWithDefaultRouteAndArea();
  34. }
  35. }
  36. }

修改Program类,新增UseAutofac:

  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Extensions.Hosting;
  3. namespace Lemon.UserCenter.HttpApi.Hosting
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. CreateHostBuilder(args).Build().Run();
  10. }
  11. public static IHostBuilder CreateHostBuilder(string[] args) =>
  12. Host.CreateDefaultBuilder(args)
  13. .ConfigureWebHostDefaults(webBuilder =>
  14. {
  15. webBuilder.UseStartup<Startup>();
  16. }).UseAutofac();
  17. }
  18. }

修改Startup类:

  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.Extensions.DependencyInjection;
  3. namespace Lemon.UserCenter.HttpApi.Hosting
  4. {
  5. public class Startup
  6. {
  7. public void ConfigureServices(IServiceCollection services)
  8. {
  9. services.AddApplication<UserCenterHttpApiHostingModule>();
  10. }
  11. public void Configure(IApplicationBuilder app)
  12. {
  13. app.InitializeApplication();
  14. }
  15. }
  16. }
  1. cd Lemon.UserCenter.HttpApi.Hosting
  2. dotnet watch run

操作如下图:

数据库结果如下:

以上就是接口服务的构建过程,主要参考了ABP CLI生成的项目结构,但是又有所不同。整个分层架构还可以继续优化,这个就见仁见智吧。后续还会继续分享ABP的相关知识,例如identity server 4、缓存、微服务等。

版权声明:本文为huangmingji原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/229015504/p/12511917.html