手把手教你用Abp vnext构建API接口服务 (2)

数据层添加引用:

dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Volo.Abp.EntityFrameworkCore dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Volo.Abp.EntityFrameworkCore.PostgreSQL dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Microsoft.EntityFrameworkCore.Design dotnet add Lemon.UserCenter.EntityFrameworkCore/Lemon.UserCenter.EntityFrameworkCore.csproj package Microsoft.EntityFrameworkCore 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类:

using Lemon.UserCenter.Domain; using Microsoft.EntityFrameworkCore; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; namespace Lemon.UserCenter.EntityFrameworkCore { [ConnectionStringName("Default")] public class UserCenterDbContext : AbpDbContext<UserCenterDbContext> { public DbSet<UserData> UserData { get; set; } public UserCenterDbContext(DbContextOptions<UserCenterDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); } } }

加入UserCenterDbContextFactory类:

using System.IO; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; namespace Lemon.UserCenter.EntityFrameworkCore { public class UserCenterDbContextFactory: IDesignTimeDbContextFactory<UserCenterDbContext> { public UserCenterDbContext CreateDbContext(string[] args) { var configuration = BuildConfiguration(); var builder = new DbContextOptionsBuilder<UserCenterDbContext>() .UseNpgsql(configuration.GetConnectionString("Default")); return new UserCenterDbContext(builder.Options); } private static IConfigurationRoot BuildConfiguration() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false); return builder.Build(); } } }

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

{ "ConnectionStrings": { "Default": "server=127.0.0.1;port=5432;Database=abp-samples-user-center;uid=postgres;pwd=123456" } }

创建数据层模块类:

using Lemon.UserCenter.Domain; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.PostgreSql; using Volo.Abp.Modularity; namespace Lemon.UserCenter.EntityFrameworkCore { [DependsOn(typeof(UserCenterDomainModule), typeof(AbpEntityFrameworkCoreModule), typeof(AbpEntityFrameworkCorePostgreSqlModule))] public class UserCenterentityFrameworkCoreModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext<UserCenterDbContext>(options => { options.AddDefaultRepositories(includeAllEntities: true); }); Configure<AbpDbContextOptions>(options => { options.Configure(ctx => { if (ctx.ExistingConnection != null) { ctx.DbContextOptions.UseNpgsql(ctx.ExistingConnection); } else { ctx.DbContextOptions.UseNpgsql(ctx.ConnectionString); } }); }); #region 自动迁移数据库 context.Services.BuildServiceProvider().GetService<UserCenterDbContext>().Database.Migrate(); #endregion 自动迁移数据库 } } }

生成数据迁移代码:

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

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

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

手把手教你用Abp vnext构建API接口服务

6. 在应用层实现具体业务逻辑

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

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

创建应用层模块类:

using Volo.Abp.Modularity; using Volo.Abp.Identity; namespace Lemon.UserCenter.Application { [DependsOn(typeof(AbpIdentityApplicationModule))] public class UserCenterApplicationModule : AbpModule { } }

创建用户接口:

using System.Threading.Tasks; using Lemon.UserCenter.Domain; namespace Lemon.UserCenter.Application { public interface IUserService { Task<UserData> Create(UserData data); } }

实现用户服务:

using System; using System.Threading.Tasks; using Lemon.UserCenter.Domain; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace Lemon.UserCenter.Application { public class UserService : ApplicationService, IUserService { private readonly IRepository<UserData, Guid> _repository; public UserService(IRepository<UserData, Guid> repository) { this._repository = repository; } public async Task<UserData> Create(UserData data) { return await _repository.InsertAsync(data); } } } 7. 在api层实现webapi控制器

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

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzyzp.html