According to the previous chapter: 【abp Vnext】Download and run the detailed tutorial document of the abp Vnext project
This sample project has been uploaded to Gitee: https://gitee.com/henrryhu/acme.-book-store
Next, demonstrate
Create entity,
build DTO,
set DTO and entity conversion mapping,
add DbSet attribute for entity,
declare open interface,
realize interface encapsulation, and
need to generate database migration after creating new entity
Here's the current solution:
1. Build entities
In Acme.BookStore.Domain
the solution, create a new folder Com, and create a new entity class in the folderMuenList
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;
namespace Acme.BookStore.Com
{
/// <summary>
/// 菜单
/// </summary>
[Table("MuenList")]
[Description("菜单表")]
public class MuenList:ComBase<Guid>,IMultiTenant
{
[Key]
public Guid Id {
get; set; }
/// <summary>
/// 菜单名称
/// </summary>
[StringLength(50)]
[Description("菜单名称")]
public string Title {
get; set; }
/// <summary>
/// 菜单名称
/// </summary>
[Description("菜单代码")]
public string Key {
get; set; }
/// <summary>
/// 菜单名称
/// </summary>
[Description("菜单图标")]
public string IconType {
get; set; }
/// <summary>
/// 菜单名称
/// </summary>
[Description("菜单路径")]
public string Path {
get; set; }
/ <summary>
/ 菜单名称
/ </summary>
//public List<MuenList> Children { get; set; }
/// <summary>
/// 租户ID
/// </summary>
[Description("租户ID")]
public Guid? TenantId {
get; set; }
}
}
2. Build DTO
Under Acme.BookStore.Application.Contracts
the solution, create a new COM folder and create a new MuenListDto
class
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
namespace Acme.BookStore.COM
{
/// <summary>
/// 菜单
/// </summary>
public class MuenListDto: ComBaseDto
{
/// <summary>
/// 菜单名称
/// </summary>
[Column("菜单名称")]
public string Title {
get; set; }
/// <summary>
/// 菜单名称
/// </summary>
[Column("菜单代码")]
public string Key {
get; set; }
/// <summary>
/// 菜单名称
/// </summary>
[Column("菜单图标")]
public string IconType {
get; set; }
/// <summary>
/// 菜单名称
/// </summary>
[Column("菜单路径")]
public string Path {
get; set; }
/// <summary>
/// 租户ID
/// </summary>
public Guid? TenantId {
get; set; }
}
}
3. Set DTO and entity conversion mapping
In Acme.BookStore.Application
the solution, enter BookStoreApplicationAutoMapperProfile
the class file
and add the following code to realize the mapping between entities and DTOs
CreateMap<MuenList, MuenListDto>().ReverseMap();
4. Add the DbSet property to the entity
Add DbSet attribute code in the class file in Acme.BookStore.EntityFrameworkCore
the solutionBookStoreDbContext
public DbSet<MuenList> MuenList {
get; set; }
5. Declare the interface open to the outside world
In Acme.BookStore.Application.Contracts
the solution, under the COM folder, add a new IMenuListAppService
class file
public interface IMenuListAppService: IApplicationService
{
/// <summary>
/// 新增接口
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<OutputBaseDto<MuenListDto>> CreateAsync(MuenListDto input);
}
6. Realize the encapsulation of the interface
In Acme.BookStore.Application
the solution, create a new COM folder, create a new MenuListAppService
file class
to implement the encapsulation code, here you can customize any business interface CRUD
using Acme.BookStore.COM;
using AutoMapper.Internal.Mappers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Users;
namespace Acme.BookStore.Com
{
public class MenuListAppService : ApplicationService, IMenuListAppService
{
public readonly IRepository<MuenList, Guid> _MenuListRepository;
public MenuListAppService(IRepository<MuenList, Guid> menuListRepository)
{
_MenuListRepository = menuListRepository;
}
/// <summary>
/// 插入返回
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<OutputBaseDto<MuenListDto>> CreateAsync(MuenListDto input)
{
var entity = ObjectMapper.Map<MuenListDto, MuenList>(input);
Console.WriteLine(entity);
await _MenuListRepository.InsertAsync(entity, autoSave: true);
OutputBaseDto<MuenListDto> output = new OutputBaseDto<MuenListDto>();
output.Data = ObjectMapper.Map<MuenList, MuenListDto>(entity);
return output;
}
}
}
7. Database migration needs to be generated after creating a new entity
Click Tools == "NuGet Package Manager == "Package Manager Console == "Select the default project as Acme.BookStore.EntityFrameworkCore
and then enter the following in the terminal to generate the database migration file
//add-migration "迁移说明"
add-migration init
Then execute it in the database
update-database
8. Notes
1. Only when a new entity is created, it is necessary to execute the generation of the migration database file
2. The encapsulation of business interfaces is mainly realized in Acme.BookStore.Application
the solution
3. How many public interfaces Acme.BookStore.Application
are implemented in the solution , which interfaces need to be declared in the solutionpublic
Acme.BookStore.Application.Contracts
4. ABP official document: https://docs.abp.io/zh-Hans/abp/latest/Object-To-Object-Mapping
5. I have started two solutions here Acme.BookStore.HttpApi.Host
, Acme.BookStore.AuthServer
which HttpApi.Host
are swagger and AuthServer
permission management