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

news/2024/10/19 6:23:21/

根据上章:【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是权限管理
在这里插入图片描述


http://www.ppmy.cn/news/37691.html

相关文章

NC271.二叉搜索树的后序遍历序列

文章目录一、题目描述二、示例三、主要思路一、题目描述 输入一个整数数组&#xff0c;判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回 true ,否则返回 false 。假设输入的数组的任意两个数字都互不相同。 提示&#xff1a; 1.二叉搜索树是指父亲节点大于左子树中…

Java多线程:Thread中的静态方法

Thread类中的静态方法 Thread类中的静态方法表示操作的线程是"正在执行静态方法所在的代码块的线程"。为什么Thread类中要有静态方法&#xff0c;这样就能对CPU当前正在运行的线程进行操作。下面来看一下Thread类中的静态方法&#xff1a; 1、currentThread() cur…

【前推回代法】含有分布式电源的三相不平衡配电网潮流计算【IEEE33节点】(Matlab代码实现)

&#x1f468;‍&#x1f393;个人主页&#xff1a;研学社的博客 &#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维…

离线数据处理 任务一:数据抽取

数据准备 hive数据库和表的创建 任务 数据准备 准备数据文件 数据获取:https://download.csdn.net/download/dafsq/87635868?spm=1001.2014.3001.5501

LFM雷达及USRP验证【章节4:USRP环境仿真】

目录 1. USRP参数 1.1 收发机各项参数 1.2 结构框图 2. URSP仿真测试 2.1 USRP仿真参数设置 2.2 LFM参数设置 2.3 matlab仿真 实验仿真环境 USRP2944*1&#xff0c;matlab2021b&#xff0c;Labview2019&#xff0c;单LFM脉冲 1. USRP参数 1.1 收发机各项参数 参考USRP…

Mac node使用nvm进行版本管理

一、Homebrew自动安装 // Homebrew安装/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"// Homebrew卸载/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"输入…

一款全新的基于GPT4的Python神器,关键还免费

chartgpt大火之后&#xff0c;随之而来的就是一大类衍生物了。 然后&#xff0c;今天要给大家介绍的是一款基于GPT4的新一代辅助编程神器——Cursor。 它最值得介绍的地方在于它免费&#xff0c;我们可以直接利用它来辅助我们编程&#xff0c;真正做到事半功倍。 注意&#…

jsp+servlet+java物流快递网站

本系统为用户提供强大的数据操纵功能&#xff0c;界面友好、使用简单方便&#xff0c;系统维护成本低。有鉴于简单操作和界面的可视化的优势。并借助于网络的优势。本系统采用JSP语言、Myeclipse开发工具&#xff0c;后端采用的是Mysql数据库来完成物流公司物流管理系统的设计与…