【ABP Vnext】实现业务接口的CRUD的操作流程

根据上章:【abp Vnext】下载并运行abp Vnext项目详细教程文档
该实例项目已上传到Gitee:https://gitee.com/henrryhu/acme.-book-store

接下来,演示

建实体、
建DTO、
设置DTO与实体互转映射、
为实体添加DbSet属性、
申明对外开放的接口、
实现接口的封装、
新建实体后需要生成数据库迁移

这是目前的解决方案:
在这里插入图片描述

1.建实体

Acme.BookStore.Domain解决方案里,新建文件夹Com,在文件夹里新建实体类MuenList
在这里插入图片描述

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.建DTO

Acme.BookStore.Application.Contracts解决方案下,新建COM文件夹,新建MuenListDto
在这里插入图片描述

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.设置DTO与实体互转映射

Acme.BookStore.Application解决方案里,进入BookStoreApplicationAutoMapperProfile类文件
在这里插入图片描述
加入如下代码,实现实体与DTO互转映射

 CreateMap<MuenList, MuenListDto>().ReverseMap();

4.为实体添加DbSet属性

Acme.BookStore.EntityFrameworkCore解决方案里BookStoreDbContext类文件里
在这里插入图片描述
新增DbSet属性代码

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

5.申明对外开放的接口

Acme.BookStore.Application.Contracts解决方案里,COM文件夹下,新增IMenuListAppService类文件

在这里插入图片描述

  public interface IMenuListAppService: IApplicationService
    {
    
    
         /// <summary>
        /// 新增接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<OutputBaseDto<MuenListDto>> CreateAsync(MuenListDto input);
    }

6.实现接口的封装

Acme.BookStore.Application解决方案里,新建COM文件夹,新建MenuListAppService文件类
在这里插入图片描述
实现封装代码,这里可以自定义任何业务接口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.新建实体后需要生成数据库迁移

点击工具==》NuGet包管理器==》程序包管理器控制台==》选择默认项目为Acme.BookStore.EntityFrameworkCore
在这里插入图片描述
然后在终端输入如下实现生成数据库迁移文件

//add-migration  "迁移说明"
add-migration init

然后执行到数据库里

update-database

在这里插入图片描述
在这里插入图片描述

8.注意点

1.只有新建了实体才需要执行生成迁移数据库文件

2.业务接口的封装主要实现在Acme.BookStore.Application解决方案里

3.在Acme.BookStore.Application解决方案里实现了多少public公开的接口,就需要在Acme.BookStore.Application.Contracts解决方案里申明有哪些接口

4.ABP官方文档:https://docs.abp.io/zh-Hans/abp/latest/Object-To-Object-Mapping

5.我这里启动了Acme.BookStore.HttpApi.HostAcme.BookStore.AuthServer两个解决方案,HttpApi.Host是swagger,AuthServer是权限管理
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43861689/article/details/129945522
ABP