[ABP Vnext] The operation process of CRUD to realize the business interface

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:
insert image description here

1. Build entities

In Acme.BookStore.Domainthe solution, create a new folder Com, and create a new entity class in the folderMuenList
insert image description here

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.Contractsthe solution, create a new COM folder and create a new MuenListDtoclass
insert image description here

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.Applicationthe solution, enter BookStoreApplicationAutoMapperProfilethe class file
insert image description here
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.EntityFrameworkCorethe solutionBookStoreDbContext
insert image description here

public DbSet<MuenList> MuenList {
    
     get; set; }

5. Declare the interface open to the outside world

In Acme.BookStore.Application.Contractsthe solution, under the COM folder, add a new IMenuListAppServiceclass file

insert image description here

  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.Applicationthe solution, create a new COM folder, create a new MenuListAppServicefile class
insert image description here
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
insert image description here
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

insert image description here
insert image description here

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.Applicationthe solution

3. How many public interfaces Acme.BookStore.Applicationare implemented in the solution , which interfaces need to be declared in the solutionpublicAcme.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.AuthServerwhich HttpApi.Hostare swagger and AuthServerpermission management
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43861689/article/details/129945522