【愚公系列】2022年06月 .NET架构班 083-微服务专题 Abp vNext基本CRUD实现
一、ABP
1.创建启动模板
abp new Acme.BookStore -t app
2.创建Book实体
在Acme.BookStore.Domain项目中创建一个 Books 文件夹(命名空间),并在其中添加名为 Book 的类,如下所示:
public class Book : AuditedAggregateRoot<Guid>
{
public string Name { get; set; }
public BookType Type { get; set; }
public DateTime PublishDate { get; set; }
public float Price { get; set; }
}
3.BookType枚举
在Acme.BookStore.Domain.Shared项目中创建Books文件夹(命名空间),并在其中添加BookType:
public enum BookType
{
Undefined,
Adventure,
Biography,
Dystopia,
Fantastic,
Horror,
Science,
ScienceFiction,
Poetry
}
4.将Book实体添加到DbContext中
EF Core需要你将实体和 DbContext 建立关联.最简单的做法是在Acme.BookStore.EntityFrameworkCore项目的BookStoreDbContext类中添加DbSet属性.如下所示:
5.将Book实体映射到数据库表
打开BookStoreDbContext类的OnModelCreating方法,为Book实体添加映射代码:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
/* Include modules to your migration db context */
builder.ConfigurePermissionManagement();
builder.ConfigureSettingManagement();
builder.ConfigureBackgroundJobs();
builder.ConfigureAuditLogging();
builder.ConfigureIdentity();
builder.ConfigureIdentityServer();
builder.ConfigureFeatureManagement();
builder.ConfigureTenantManagement();
builder.Entity<Book>(b =>
{
b.ToTable(BookStoreConsts.DbTablePrefix + "Books",
BookStoreConsts.DbSchema);
b.ConfigureByConvention(); //auto configure for the base class props
b.Property(x => x.Name).IsRequired().HasMaxLength(128);
});
/* Configure your own tables/entities inside here */
//builder.Entity<YourEntity>(b =>
//{
// b.ToTable(BookStoreConsts.DbTablePrefix + "YourEntities", BookStoreConsts.DbSchema);
// b.ConfigureByConvention(); //auto configure for the base class props
// //...
//});
}
6.添加数据迁移
在 Acme.BookStore.EntityFrameworkCore 目录打开命令行终端输入以下命令:
dotnet ef migrations add Created_Book_Entity
7.添加种子数据
在 *.Domain 项目下创建BookStoreDataSeederContributor
public class BookStoreDataSeederContributor
: IDataSeedContributor, ITransientDependency
{
private readonly IRepository<Book, Guid> _bookRepository;
public BookStoreDataSeederContributor(IRepository<Book, Guid> bookRepository)
{
_bookRepository = bookRepository;
}
public async Task SeedAsync(DataSeedContext context)
{
if (await _bookRepository.GetCountAsync() <= 0)
{
await _bookRepository.InsertAsync(
new Book
{
Name = "1984",
Type = BookType.Dystopia,
PublishDate = new DateTime(1949, 6, 8),
Price = 19.84f
},
autoSave: true
);
await _bookRepository.InsertAsync(
new Book
{
Name = "The Hitchhiker's Guide to the Galaxy",
Type = BookType.ScienceFiction,
PublishDate = new DateTime(1995, 9, 27),
Price = 42.0f
},
autoSave: true
);
}
}
}
8.更新数据库
运行 Acme.BookStore.DbMigrator 应用程序来更新数据库:
9.创建BookDto
在 Acme.BookStore.Application.Contracts 项目中创建 Books 文件夹(命名空间), 并在其中添加名为 BookDto 的DTO类:
public class BookDto : AuditedEntityDto<Guid>
{
public string Name { get; set; }
public BookType Type { get; set; }
public DateTime PublishDate { get; set; }
public float Price { get; set; }
}
在Acme.BookStore.Application项目的BookStoreApplicationAutoMapperProfile类中定义映射:
CreateMap<Book, BookDto>();
10.创建CreateUpdateBookDto
在Acme.BookStore.Application.Contracts项目中创建 Books 文件夹(命名空间),并在其中添加名为 CreateUpdateBookDto 的DTO类:
public class CreateUpdateBookDto
{
[Required]
[StringLength(128)]
public string Name { get; set; }
[Required]
public BookType Type { get; set; } = BookType.Undefined;
[Required]
[DataType(DataType.Date)]
public DateTime PublishDate { get; set; } = DateTime.Now;
[Required]
public float Price { get; set; }
}
在Acme.BookStore.Application项目的BookStoreApplicationAutoMapperProfile类中定义映射:
CreateMap<CreateUpdateBookDto, Book>();
11.IBookAppService
在Acme.BookStore.Application.Contracts项目创建 Books 文件夹(命名空间),并在其中添加名为IBookAppService的接口:
public interface IBookAppService :
ICrudAppService< //Defines CRUD methods
BookDto, //Used to show books
Guid, //Primary key of the book entity
PagedAndSortedResultRequestDto, //Used for paging/sorting
CreateUpdateBookDto> //Used to create/update a book
{
}
12.BookAppService
在Acme.BookStore.Application项目中创建 Books 文件夹(命名空间),并在其中添加名为 BookAppService 的类:
public class BookAppService :
CrudAppService<
Book, //The Book entity
BookDto, //Used to show books
Guid, //Primary key of the book entity
PagedAndSortedResultRequestDto, //Used for paging/sorting
CreateUpdateBookDto>, //Used to create/update a book
IBookAppService //implement the IBookAppService
{
public BookAppService(IRepository<Book, Guid> repository)
: base(repository)
{
}
}
13.运行程序
访问swagger文档:https://localhost:44350/swagger/index.html
- 点赞
- 收藏
- 关注作者
评论(0)