NET7中sqlsugar的使用
仿《深入浅出ASP.NET CORE》这书里的IRepository和RepositoryBase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using SqlSugar; using System.Linq.Expressions; namespace WebApplication1.DAL { /// <summary> /// 所有仓储的约定,此接口仅作为约定,用于标识他们 /// </summary> /// <typeparam name="TEntity">传入仓储的实体模型</typeparam> /// <typeparam name="TPrimaryKey">传入仓储的主键类型</typeparam> public interface IRepository<TEntity, TPrimaryKey> where TEntity : class { #region 查询 ISugarQueryable<TEntity> GetAll(); List<TEntity> GetAllList(); Task<List<TEntity>> GetAllListAsync(); List<TEntity> GetAllList(Expression<Func<TEntity, bool >> predicate); Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool >> predicate); TEntity FirstOrDefault(Expression<Func<TEntity, bool >> predicate); Task<TEntity> FirstOrDefaultAsycn(Expression<Func<TEntity, bool >> predicate); #endregion #region 新增 void Insert(TEntity entity); Task InsertAsync(TEntity entity); #endregion #region 更新 void Update(TEntity entity); Task UpdateAsync(TEntity entity); #endregion #region 删除 void Delete(TEntity entity); Task DeleteAsync(TEntity entity); void Delete(Expression<Func<TEntity, bool >> predicate); Task DeleteAsync(Expression<Func<TEntity, bool >> predicate); #endregion #region 总和计算 int Count(); Task< int > CountAsync(); int Count(Expression<Func<TEntity, bool >> predicate); Task< int > CountAsync(Expression<Func<TEntity, bool >> predicate); #endregion } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | using System.Collections.Generic; using System.Linq.Expressions; using System; using SqlSugar; namespace WebApplication1.DAL { /// <summary> /// 默认仓储的通用功能实现,用于所有的领域模型 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TPrimaryKey"></typeparam> public class RepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey> where TEntity : class { protected readonly ISqlSugarClient db; public RepositoryBase(ISqlSugarClient db) { this .db=db; } public int Count() { return GetAll().Count(); } public int Count(Expression<Func<TEntity, bool >> predicate) { return GetAll().Where(predicate).Count(); } public async Task< int > CountAsync() { return await GetAll().CountAsync(); } public async Task< int > CountAsync(Expression<Func<TEntity, bool >> predicate) { return await GetAll().Where(predicate).CountAsync(); } public void Delete(TEntity entity) { db.DeleteableByObject(entity).ExecuteCommand(); } public async Task DeleteAsync(TEntity entity) { await db.DeleteableByObject(entity).ExecuteCommandAsync(); } public void Delete(Expression<Func<TEntity, bool >> predicate) { foreach ( var entity in GetAll().Where(predicate).ToList()) { Delete(entity); } } public async Task DeleteAsync(Expression<Func<TEntity, bool >> predicate) { foreach ( var entity in GetAll().Where(predicate).ToList()) { await DeleteAsync(entity); } } public TEntity FirstOrDefault(Expression<Func<TEntity, bool >> predicate) { return GetAll().First(predicate); } public async Task<TEntity> FirstOrDefaultAsycn(Expression<Func<TEntity, bool >> predicate) { return await GetAll().FirstAsync(predicate); } public ISugarQueryable<TEntity> GetAll() { return db.Queryable<TEntity>(); } public List<TEntity> GetAllList() { return GetAll().ToList(); } public List<TEntity> GetAllList(Expression<Func<TEntity, bool >> predicate) { return GetAll().Where(predicate).ToList(); } public async Task<List<TEntity>> GetAllListAsync() { return await GetAll().ToListAsync(); } public async Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool >> predicate) { return await GetAll().Where(predicate).ToListAsync(); } public void Insert(TEntity entity) { db.InsertableByObject(entity).ExecuteCommand(); } public async Task InsertAsync(TEntity entity) { await db.InsertableByObject(entity).ExecuteCommandAsync(); } public void Update(TEntity entity) { db.UpdateableByObject(entity).ExecuteCommand(); } public async Task UpdateAsync(TEntity entity) { await db.UpdateableByObject(entity).ExecuteCommandAsync(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | //注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行 builder.Services.AddHttpContextAccessor(); //注册SqlSugar builder.Services.AddSingleton<ISqlSugarClient>(s => { SqlSugarScope sqlSugar = new SqlSugarScope( new ConnectionConfig() { DbType = SqlSugar.DbType.SqlServer, ConnectionString = "server=.\\sqlexpress;uid=sa;pwd=123456;database=studentdb;pooling=true;min pool size=5;max pool size=100;TrustServerCertificate=true;" , IsAutoCloseConnection = true , }, db => { //单例参数配置,所有上下文生效 db.Aop.OnLogExecuting = (sql, pars) => { //获取IOC对象不要求在一个上下文 //vra log=s.GetService<Log>() //获取IOC对象要求在一个上下文 //var appServive = s.GetService<IHttpContextAccessor>(); //var log= appServive?.HttpContext?.RequestServices.GetService<Log>(); }; }); return sqlSugar; }); builder.Services.AddTransient( typeof (IRepository<,>), typeof (RepositoryBase<,>)); |
撸码:复制、粘贴,拿起键盘就是“干”!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具