.net core 实例教程(三)仓储及领域服务功能实现(既实现用户表的增删改查接口)
本文源码下载地址:http://www.80cxy.com/Blog/ResourceView?arId=202403191532545995NAAqJh
系列教程地址:http://www.80cxy.com/Blog/ArticleView?arId=202403191517574161ay3s5V
本文实现用户表的增删改查操作,主要分为仓储实现、领域服务实现以及注册服务功能。
仓储封装了基础设施来提供查询和持久化聚合操作。它们集中提供常见的数据访问功能,从而提供更好的可维护性,并将用于访问数据库的基础结构或技术与领域层分离。创建数据访问层和应用程序的业务逻辑层之间的抽象层。
领域服务主要实现业务逻辑代码。
一、仓储实现
在SignUp.Domain项目创建ISystemDomainRepostory仓储接口,并在SignUp.Infrastructure项目中实现ISystemDomainRepostory接口。具体代码如下:
using Microsoft.EntityFrameworkCore; using SignUp.Common.DataFilter; using SignUp.Common.Enum; using SignUp.Domain.Entities; using SignUp.Domain.Repository; using System.Linq.Expressions; namespace SignUp.Infrastructure.Repository { public class SystemDomainRepostory : ISystemDomainRepostory { public readonly SignUpDbContext _dbContext; public SystemDomainRepostory(SignUpDbContext dbContext) { this._dbContext = dbContext; } #region 用户管理 public async Task<SysUser?> FindOneUserById(string id) { return await _dbContext.SysUser.FindAsync(id); } public async Task<(SysUser[], int total)> GetUserList(int pageIndex, int pageSize, string wheres, string sort, string order) { //where查询条件 List<DataFilter> searchParametersList = FormatParameters.GetSearchParameters(wheres); //order排序条件 Dictionary<string, QueryOrderBy> orderBy = FormatParameters.GetOrderParameters(sort, order); //构造查询条件 Expression<Func<SysUser, bool>> expression = DataFilterConvertor<SysUser>.ToExpression(searchParametersList); var listAll = _dbContext.SysUser.Where(expression); int total = listAll.Count(); var list = await listAll.OrderConditions(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArrayAsync(); return (list, total); } public async Task<SysUser> AddUser(string userName, string password, string realName, string userType) { SysUser model = SysUser.Add(userName, password, realName, userType); await _dbContext.SysUser.AddAsync(model); await _dbContext.SaveChangesAsync(); return model; } public async Task<SysUser> EditUser(string id, string userName, string realName, string userType) { var model = await FindOneUserById(id); model?.Edit(userName, realName, userType); await _dbContext.SaveChangesAsync(); return model; } public async Task DeleteUser(string id) { var model = await _dbContext.SysUser.FindAsync(Guid.Parse(id)); if (model != null) { _dbContext.SysUser.Remove(model); } } public void DeleteUserRange(string[] ids) { var list = _dbContext.SysUser.Where(x => ids.Contains(x.Id.ToString())).ToArray(); if (list.Length != 0) { _dbContext.SysUser.RemoveRange(list); } } #endregion } }
二、领域服务实现
在SignUp.Domain项目创建领域服务类SystemDomainService,具体代码如下:
using SignUp.Common.Commons; using SignUp.Common.Enum; using SignUp.Domain.Entities; using SignUp.Domain.Repository; namespace SignUp.Domain.Service { public class SystemDomainService : ISystemDomainService { private ISystemDomainRepostory _systemDomainRepostory; public SystemDomainService(ISystemDomainRepostory systemDomainRepostory) { _systemDomainRepostory = systemDomainRepostory; } public async Task<ResponseContent> GetUserList(int pageIndex, int pageSize, string wheres, string sort, string order) { ResponseContent response = new ResponseContent(); (var list, int total) = await _systemDomainRepostory.GetUserList(pageIndex, pageSize, wheres, sort, order); return response.Ok(new { total = total, rows = list }); } public async Task<ResponseContent> AddUser(string userName, string password, string realName, string userType) { ResponseContent response = new ResponseContent(); await _systemDomainRepostory.AddUser(userName, password, realName, userType); return response.Ok(); } public async Task<ResponseContent> EditUser(string id, string userName, string realName, string userType) { ResponseContent response = new ResponseContent(); var model = await _systemDomainRepostory.EditUser(id, userName, realName, userType); return response.Ok(); } public async Task<ResponseContent> DeleteUser(string id) { ResponseContent response = new ResponseContent(); await _systemDomainRepostory.DeleteUser(id); return response.Ok(); } public ResponseContent DeleteUserRange(string[] ids) { ResponseContent response = new ResponseContent(); _systemDomainRepostory.DeleteUserRange(ids); return response.Ok(); } } }
三、注册仓储、领域服务
为避免所有内容到入口项目中注册,每个项目中自己实现IModuleInitializer接口的类,并在其中注册自己需要的服务。然后通过ModuleInitializerExtensions注册到IServiceCollection中去。
相关代码如下:
using Microsoft.Extensions.DependencyInjection; namespace SignUp.Common.Commons { /// <summary> /// 所有项目中的实现了IModuleInitializer接口都会被调用,请在Initialize中编写注册本模块需要的服务。 /// 一个项目中可以放多个实现了IModuleInitializer的类。不过为了集中管理,还是建议一个项目中只放一个实现了IModuleInitializer的类 /// </summary> public interface IModuleInitializer { public void Initialize(IServiceCollection services); } } using Microsoft.Extensions.DependencyInjection; using SignUp.Common.Commons; using SignUp.Domain.Repository; using SignUp.Domain.Service; using SignUp.Infrastructure.Repository; namespace SignUp.Infrastructure { class ModuleInitializer : IModuleInitializer { public void Initialize(IServiceCollection services) { services.AddScoped<ISystemDomainService, SystemDomainService>(); services.AddScoped<ISystemDomainRepostory, SystemDomainRepostory>(); } } } using Microsoft.Extensions.DependencyInjection; using System.Reflection; namespace SignUp.Common.Commons { public static class ModuleInitializerExtensions { /// <summary> /// 每个项目中都可以自己写一些实现了IModuleInitializer接口的类,在其中注册自己需要的服务,这样避免所有内容到入口项目中注册 /// </summary> /// <param name="services"></param> /// <param name="assemblies"></param> public static IServiceCollection RunModuleInitializers(this IServiceCollection services, IEnumerable<Assembly> assemblies) { foreach (var asm in assemblies) { Type[] types = asm.GetTypes(); var moduleInitializerTypes = types.Where(t => !t.IsAbstract && typeof(IModuleInitializer).IsAssignableFrom(t)); foreach (var implType in moduleInitializerTypes) { var initializer = (IModuleInitializer?)Activator.CreateInstance(implType); if (initializer == null) { throw new ApplicationException($"Cannot create ${implType}"); } initializer.Initialize(services); } } return services; } } }
学习交流
附笔者学习 .net core开发时参考相关项目实例源码:asp.net core webapi项目实例源代码锦集下载(72个)
猜您可能还喜欢
- net core+webapi+nginx windows 服务器部署(1321)
- .Nuget Packages 太占C盘,删除后可以放到其他盘(1194)
- ASP.NET Core 配置 Swagger 显示接口注释描述信息(1094)
- vue调用接口后获取不到后端返回的Header响应头(964)
- .net core 系列实例开发教程-权限管理系统功能介绍(952)
- .net core 6.0 web API + SwaggerUI + IIS部署(912)
- .net core 实例教程(十二)配置启用Swagger中的【Authorize】按钮(831)
- .net core 实例教程(一)新建项目(797)
- .net core 实例教程(十四)配置 Swagger 显示接口注释描述信息及支持版本控制(783)
- .net 6中使用log4net写sqlserver数据库日志(756)
评论列表
发表评论
文章分类
文章归档
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
阅读排行
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5702)
- 2.各大搜索网站网站收录提交入口地址(3201)
- 3.Windows 10休眠文件更改存储位置(3163)
- 4.ECharts仪表盘实例及参数使用详解(3095)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(2991)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(2866)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(2779)
- 8.CERT_HAS_EXPIRED错误如何解决(2245)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2188)
- 10.HBuilder编辑器格式化代码(2118)