// namespace SafeCampus.SqlSugar; /// /// 仓储模式对象 /// [SuppressSniffer] public class DbRepository : SimpleClient where T : class, new() { protected ITenant Tenant;//多租户事务、GetConnection、IsAnyConnection等功能 public DbRepository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null { Context = DbContext.DB.GetConnectionScopeWithAttr();//ioc注入的对象 Tenant = DbContext.DB; } #region 仓储方法拓展 #region 插入 /// /// 批量插入判断走普通导入还是大数据 /// /// 数据 /// 阈值 /// public virtual async Task InsertOrBulkCopy(List data, int threshold = 10000) { if (data.Count > threshold) return await Context.Fastest().BulkCopyAsync(data);//大数据导入 return await Context.Insertable(data).ExecuteCommandAsync();//普通导入 } #endregion 插入 #region 列表 /// /// 获取列表指定多个字段 /// /// 查询条件 /// 查询字段 /// public virtual Task> GetListAsync(Expression> whereExpression, Expression> selectExpression) { return Context.Queryable().Where(whereExpression).Select(selectExpression).ToListAsync(); } /// /// 获取列表指定单个字段 /// /// 查询条件 /// 查询字段 /// public virtual Task> GetListAsync(Expression> whereExpression, Expression> selectExpression) { return Context.Queryable().Where(whereExpression).Select(selectExpression).ToListAsync(); } /// /// 获取列表指定单个字段 /// /// 查询条件 /// 查询字段 /// public virtual Task> GetListAsync(Expression> whereExpression, Expression> selectExpression) { return Context.Queryable().Where(whereExpression).Select(selectExpression).ToListAsync(); } #endregion 列表 #region 单查 /// /// 获取指定表的单个字段 /// /// 查询条件 /// 查询字段 /// public virtual Task GetFirstAsync(Expression> whereExpression, Expression> selectExpression) { return Context.Queryable().Where(whereExpression).Select(selectExpression).FirstAsync(); } /// /// 获取指定表的单个字段 /// /// 查询条件 /// 查询字段 /// protected virtual Task GetFirstAsync(Expression> whereExpression, Expression> selectExpression) { return Context.Queryable().Where(whereExpression).Select(selectExpression).FirstAsync(); } /// /// 根据条件查询获取自动分表的单个数据 /// /// 条件表达式 /// 开始时间 /// 结束时间 /// 实体 public virtual Task GetFirstSplitTableAsync(Expression> whereExpression, DateTime startTime, DateTime endTime) { return Context.Queryable().Where(whereExpression).SplitTable(startTime, endTime).FirstAsync(); } /// /// 根据条件查询获取自动分表的单个数据 /// /// 条件表达式 /// 分表查询表达式 /// 实体 public virtual Task GetFirstSplitTableAsync(Expression> whereExpression, Func, IEnumerable> getTableNamesFunc) { return Context.Queryable().Where(whereExpression).SplitTable(getTableNamesFunc).FirstAsync(); } #endregion 单查 #endregion 仓储方法拓展 }