Net8 ABP VNext完美集成FreeSql、SqlSugar,实现聚合根增删改查,完全去掉EFCore

没有基础的,请参考上一篇

彩蛋到最后一张图里找

参考链接

结果直接上图,没有任何业务代码

启动后,已经有了基本的CRUD功能,还扩展了批量删除,与动态查询

动态查询截图,支持分页,排序

实现原理:

FreeSql导航参考官方地址

聚合根(实验室) | FreeSql 官方文档

继承IReadOnlyRepository接口,实现用FreeSql实现所有功能即可

关键CRUD代码

 //默认删除public Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default){return FreeSql.Delete<T>(new { Id = id }).ExecuteAffrowsAsync(cancellationToken);}public Task DeleteDirectAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default){return FreeSql.Delete<T>().Where(predicate).ExecuteAffrowsAsync(cancellationToken);}//批量删除public Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default){return FreeSql.Delete<T>(ids).ExecuteAffrowsAsync(cancellationToken);}//默认Getpublic Task<T> GetAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default){object dywhere = new { Id = id };var query = FreeSql.GetAggregateRootRepository<T>().Select.WhereDynamic(dywhere);// FreeSql.Queryable<T>().WhereDynamic(dywhere);              FreeSqlHelper.SetNavigate(query);return query.ToOneAsync(cancellationToken);}//默认GetListpublic Task<IQueryable<T>> GetQueryableAsync(){ISelect<T> queryable;if (_httpContextAccessor.HttpContext.Request.Query.Any(q => q.Key == "Sorting")){string sorting = _httpContextAccessor.HttpContext.Request.Query["Sorting"];queryable = FreeSql.Queryable<T>().OrderBy(sorting);}else{queryable = FreeSql.Queryable<T>();}FreeSqlHelper.SetNavigate(queryable);return Task.FromResult(queryable.AsQueryable());}//默认Postpublic async Task<T> InsertAsync(T entity, bool autoSave = false, CancellationToken cancellationToken = default){SetEntity(entity);await FreeSql.GetAggregateRootRepository<T>().InsertAsync(entity,cancellationToken);return entity;}

FreeSqlHelper.cs代码

   public class FreeSqlHelper{/// <summary>/// 设置导航/// </summary>/// <typeparam name="T"></typeparam>/// <param name="query"></param>public static void SetNavigate<T>(FreeSql.ISelect<T> query) {var type= typeof(T);MemberInfo[] myMembers = type.GetProperties();foreach (MemberInfo myMember in myMembers){var navigateAttribute = myMember.GetCustomAttribute<FreeSql.DataAnnotations.NavigateAttribute>();if (navigateAttribute != null){query.IncludeByPropertyName(myMember.Name);}}}}

明细表id使用雪花漂移算法生成,引用Yitter.IdGenerator库(请自行nuget下载)

调用YitIdHelper.NextId()生成,没有数据库的自增字段功能,使用自增的问题很多

新增BaseCrudAppService类,代码直接从官方的CrudAppService里复制即可,利用批量替换,把CrudAppService替换为BaseCrudAppService

如图

新增FilterAsync与DeleteBulkAsync实现动态查询与批量删除功能

新增后的结果如图

freesql动态查询功能很强大,日期区间支持年,月,日期等,请参见下图示例说明

SearchCondition代码

    public class SearchCondition{/// <summary>/// 动态过滤条件/// </summary>public DynamicFilterInfo FilterInfo { get; set; }=new();/// <summary>/// 当前页/// </summary>public int CurrentPage { get; set; } = 1;/// <summary>/// 每页显示记录条数/// </summary>public int PageSize { get; set; } = 50;/// <summary>/// 排序/// </summary>public string Sorting { get; set; } = string.Empty;}

IBaseRepository代码如图,只是为了在BaseCrudAppService能获取到freesql

Enum实体类代码,由代码生成器生成

[Serializable]
[Table("TSYS_Enum")]
public class Enum : BaseAuditedAggregateRoot<Guid>
{/// <summary>/// 字典群组/// </summary>public int EnumGroup { get; set; }/// <summary>/// 字典类型/// </summary>public int EnumType { get; set; } = 1;/// <summary>/// 字典代码/// </summary>[StringLength(100)]public string EnumCode { get; set; }/// <summary>/// 说明/// </summary>[StringLength(100)]public string EnumDesc { get; set; }/// 备注/// </summary>[StringLength(500)]public string Remark { get; set; }/// <summary>/// 数据状态 0:未提交,1:审核中,2:已审核/// </summary>public byte Status { get; set; }/// <summary>/// 禁用状态/// </summary>public byte ForbidStatus { get; set; }/// <summary>/// 禁用人/// </summary>public Guid? ForbidderId { get; set; }/// <summary>/// 禁用日期/// </summary>public DateTime? ForbidDate { get; set; }/// <summary>/// 审核人/// </summary>public Guid? ApproverId { get; set; }/// <summary>/// 审核日期/// </summary>public DateTime? ApproveDate { get; set; }[FreeSql.DataAnnotations.Navigate(nameof(EnumItem.EnumId))]public virtual List<EnumItem> Details { get; set; }

明细表实体类

 [Serializable][Table("TSYS_EnumItem")]public class EnumItem : CreationAuditedEntity<long>{/// <summary>/// 主表key/// </summary>public Guid EnumId { get; set; }/// <summary>/// 显示值/// </summary>[StringLength(100)]public string EnumItemName { get; set; }/// <summary>/// 存储值/// </summary>[StringLength(100)]public string EnumItemValue { get; set; }/// <summary>/// 说明/// </summary>[StringLength(100)]public string EnumItemDesc { get; set; }/// <summary>/// 行号/// </summary>public int Num { get; set; } = 1;/// 备注/// </summary>[StringLength(500)]public string Remark { get; set; }/// <summary>/// 数据状态 0:未提交,1:审核中,2:已审核/// </summary>public byte Status { get; set; }/// <summary>/// 禁用状态/// </summary>public byte ForbidStatus { get; set; }/// <summary>/// 禁用人/// </summary>public Guid? ForbidderId { get; set; }/// <summary>/// 禁用日期/// </summary>public DateTime? ForbidDate { get; set; }/// <summary>/// 审核人/// </summary>public Guid? ApproverId { get; set; }/// <summary>/// 审核日期/// </summary>public DateTime? ApproveDate { get; set; }public virtual Enum Enum { get; set; }        }

把原来的CrudAppService改为BaseCrudAppService即可,这样只要继承了BaseCrudAppService所有的业务层,都有了,爽不爽啊,再也不用辛苦的做码农了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/773429.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

强化学习及其在机器人任务规划中的进展与分析

源自&#xff1a;模式识别与人工智能 作者&#xff1a;张晓明 高士杰 姚昌瑀 褚誉 彭硕 “人工智能技术与咨询” 发布 摘要 强化学习可以让机器人通过与环境的交互,学习最优的行动策略,是目前机器人领域关注的重要前沿方向之一.文中简述机器人任务规划问题的形式化建模…

《青少年成长管理2024》 003 “你将面临一个怎样的世界”

《青少年成长管理2024》 003 “你将面临一个怎样的世界” 一、审视你将面临的世界二、机器替代人类劳动三、人工智能将给这个世界带来怎样的影响本节摘要 一个生命降临世间&#xff0c;首要的任务是充分理解所面临的现实世界&#xff0c;这是做出明智选择的基础。机器替代人类的…

Unity编辑器功能将AB资源文件生成MD5码

将路径Application.dataPath/ArtRes/AB/PC文件夹下所有的Ab包文件生成MD5吗&#xff0c;通过文件名 文件长度MD5‘|’的格式拼接成字符串写入到资源对比文件abCompareInfo.txt中。 将路径pathFile扥文件生成MD5码

STM32之HAL开发——DMA转运串口数据

DMA功能框图&#xff08;F1系列&#xff09; 如果外设要想通过 DMA 来传输数据&#xff0c;必须先给 DMA 控制器发送 DMA 请求&#xff0c; DMA 收到请求信号之后&#xff0c;控制器会给外设一个应答信号&#xff0c;当外设应答后且 DMA 控制器收到应答信号之后&#xff0c;就会…

实现ls -l 功能,index,rindex函数的使用

index();----------------------------------------------------------------- index第一次遇到字符c&#xff0c;rindex最后一次遇到字符c&#xff0c;返回值都是从那个位置开始往后的字符串地址 #include <stdio.h> #include <sys/types.h> #include <pwd.h&g…

[HackMyVM]靶场Crossbow

kali:192.168.56.104 靶机:192.168.56.136 端口扫描 # nmap 192.168.56.136 Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-03-26 22:17 CST Nmap scan report for crossbow.hmv (192.168.56.136) Host is up (0.0057s latency). Not shown: 997 closed tcp…

反射率光纤光谱仪检测汽车后视镜反射率

反射率光纤光谱仪是一种用于测量材料表面反射率的精密仪器&#xff0c;它通过光纤传输光信号&#xff0c;并利用光谱仪进行分析&#xff0c;以确定材料的光学特性。反射率光纤光谱仪的工作原理基于相对反射率的计算&#xff0c;它涉及到光源、光纤、光谱仪等关键组件。 后视镜能…

牛客小白月赛89(A~C)

小白赛怎么这么难打&#xff0c;是什么小白&#xff0c;我的世界小白吗。 A. 伊甸之花 给你一个数组 a&#xff0c;问你是否找出一个 不等于 a 的数组 b&#xff0c;满足 其中数值都要在 [1,m] 的范围内 直接在 a 数组上修改&#xff0c;可以发现如果改了 a[1],a[2]&#xff…

HTML(一)---【基础】

零.前言&#xff1a; 本文章对于HTML的基础知识处理的十分细节&#xff0c;适合从头学习的初学者&#xff0c;亦或是想要提升基础的前端工程师。 1.什么是HTML&#xff1f; HTML是&#xff1a;“超文本标签语言”&#xff08;Hyper Text Markup Language&#xff09; HTML不…

《Django项目》day4 -- 部署nginx与对接acapp

文章目录 1.增加容器的映射端口&#xff1a;80&#xff08;http&#xff09;与443&#xff08;https&#xff09;2.创建AcApp&#xff0c;获取域名、nginx配置文件及https证书3.修改django项目的配置4.配置uwsgi 1.增加容器的映射端口&#xff1a;80&#xff08;http&#xff0…

SQL109 纠错4(组合查询,order by..)

SELECT cust_name, cust_contact, cust_email FROM Customers WHERE cust_state MI UNION SELECT cust_name, cust_contact, cust_email FROM Customers WHERE cust_state IL ORDER BY cust_name;order by子句&#xff0c;必须位于最后一条select语句之后

【AI模型-机器学习工具部署】远程服务器配置Jupyter notebook或jupyter lab服务

随着AI人工智能的崛起&#xff0c;机器学习、深度学习、模型训练等技术也慢慢泛化&#xff0c;java开发有idea&#xff0c;web开发有vscode&#xff0c;那么AI开发神器肯定离不开jupyter lab&#xff08;基础版jupyter notebook&#xff09; Jupyter notebook部署 1. 安装jupy…

FastAPI+React全栈开发06 使用MongoDB设置文档存储

Chapter02 Setting Up the Document Store with MongoDB 01 Summary FastAPIReact全栈开发06 使用MongoDB设置文档存储 In this chapter, we are going to address some of the main features of MongoDB, building upon what was mentioned in the introductory chapter, a…

类模板与继承及成员、全局函数的实现

一、类模板与继承 当类模板碰到继承时&#xff0c;需要注意一下几点&#xff1a; 1.当子类继承的父类是一个类模板时&#xff0c;子类在声明的时候&#xff0c;要指定出父类中T的类型 2.如果不指定&#xff0c;编译器无法给子类分配内存 3.如果想灵活指定出父类中T的类型&a…

在Jetson Nano上使用TensorRT来加速模型

NVIDIA Jetson Nano是一款小型的AI计算设备&#xff0c;专为边缘计算设计&#xff0c;适合运行机器学习和深度学习模型。TensorRT是NVIDIA的一个高性能深度学习推理&#xff08;Inference&#xff09;优化器和运行时库&#xff0c;可以用于加速深度学习模型的推理速度。 在Jet…

QT_day5:使用定时器实现闹钟

1、 程序代码&#xff1a; widget.h&#xff1a; #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QTime>//时间类 #include <QTimer>//时间事件类 #include <QTextToSpeech>//文本转语音类 QT_BEGIN_NAMESPACE namespace Ui { cla…

速盾:vue可以用cdn吗

Vue可以使用CDN&#xff08;Content Delivery Network&#xff09;来引入&#xff0c;这是一种分发网络&#xff0c;可以加速网站或应用的静态资源加载&#xff0c;从而提供更快的用户体验。在使用CDN之前&#xff0c;我们需要了解一些基本概念和步骤。 CDN是一个分布式系统&a…

2014年认证杯SPSSPRO杯数学建模C题(第一阶段)土地储备方案的风险评估全过程文档及程序

2014年认证杯SPSSPRO杯数学建模 C题 土地储备方案的风险评估 原题再现&#xff1a; 土地储备&#xff0c;是指市、县人民政府国土资源管理部门为实现调控土地市场、促进土地资源合理利用目标&#xff0c;依法取得土地&#xff0c;进行前期开发、储存以备供应土地的行为。土地…

【C语言】预处理编译链接调试技巧详解

主页&#xff1a;醋溜马桶圈-CSDN博客 专栏&#xff1a;C语言_醋溜马桶圈的博客-CSDN博客 gitee&#xff1a;mnxcc (mnxcc) - Gitee.com 目录 1.预处理 1.1 预定义符号 1.2 #define 1.2.1 #define 定义标识符 1.2.2 #define 定义宏 1.2.3 #define 替换规则 1.2.4 #和## …

HTTPS 从懵懵懂懂到认知清晰、从深度理解到落地实操

Https 在现代互联网应用中&#xff0c;网上诈骗、垃圾邮件、数据泄露的现象时有发生。为了数据安全&#xff0c;我们都会选择采用https技术。甚至iOS开发调用接口的时候&#xff0c;必须是https接口&#xff0c;才能调用。现在有部分浏览器也开始强制要求网站必须使用https&am…