优化数据仓储

This commit is contained in:
不做码农
2022-03-19 08:04:08 +08:00
parent 3e6d2fdce1
commit 1c3cdb99e0
29 changed files with 86 additions and 699 deletions

View File

@@ -15,16 +15,17 @@ namespace ZR.Repository
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
public class BaseRepository<T> : SimpleClient<T> where T : class, new()
{
public ISqlSugarClient Context;
public BaseRepository(ISqlSugarClient client = null)
public ITenant itenant = null;//多租户事务
public BaseRepository(ISqlSugarClient client = null) : base(client)
{
//通过特性拿到ConfigId
var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;
if(configId != null)
if (configId != null)
{
Context = DbScoped.SugarScope.GetConnection(configId);
itenant = DbScoped.SugarScope;//设置租户接口
}
else
{
@@ -53,10 +54,10 @@ namespace ZR.Repository
{
return Context.Insertable(t).ExecuteCommand();
}
public long InsertReturnBigIdentity(T t)
{
return Context.Insertable(t).ExecuteReturnBigIdentity();
}
//public long InsertReturnBigIdentity(T t)
//{
// return Context.Insertable(t).ExecuteReturnBigIdentity();
//}
//public int InsertIgnoreNullColumn(List<T> t)
//{
@@ -194,14 +195,38 @@ namespace ZR.Repository
public DbResult<bool> UseTran(Action action)
{
var result = Context.Ado.UseTran(() => action());
return result;
try
{
var result = Context.Ado.UseTran(() => action());
return result;
}
catch (Exception ex)
{
Context.Ado.RollbackTran();
Console.WriteLine(ex.Message);
throw;
}
}
/// <summary>
///
/// </summary>
/// <param name="client"></param>
/// <param name="action">增删改查方法</param>
/// <returns></returns>
public DbResult<bool> UseTran(SqlSugarClient client, Action action)
{
var result = client.Ado.UseTran(() => action());
return result;
try
{
var result = client.AsTenant().UseTran(() => action());
return result;
}
catch (Exception ex)
{
client.AsTenant().RollbackTran();
Console.WriteLine(ex.Message);
throw;
}
}
public bool UseTran2(Action action)
@@ -216,16 +241,6 @@ namespace ZR.Repository
return Context.Deleteable<T>();
}
/// <summary>
/// 删除表达式
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public int Delete(Expression<Func<T, bool>> expression)
{
return Context.Deleteable<T>().Where(expression).ExecuteCommand();
}
/// <summary>
/// 批量删除
/// </summary>
@@ -258,32 +273,6 @@ namespace ZR.Repository
return Context.Queryable<T>();
}
//public ISugarQueryable<ExpandoObject> Queryable(string tableName, string shortName)
//{
// return Context.Queryable(tableName, shortName);
//}
public List<T> GetList(Expression<Func<T, bool>> expression)
{
return Context.Queryable<T>().Where(expression).ToList();
}
//public Task<List<T>> QueryableToListAsync(Expression<Func<T, bool>> expression)
//{
// return Context.Queryable<T>().Where(expression).ToListAsync();
//}
//public string QueryableToJson(string select, Expression<Func<T, bool>> expressionWhere)
//{
// var query = base.Context.Queryable<T>().Select(select).Where(expressionWhere).ToList();
// return query.JilToJson();
//}
//public List<T> QueryableToList(string tableName)
//{
// return Context.Queryable<T>(tableName).ToList();
//}
public (List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, int pageIndex = 0, int pageSize = 10)
{
int totalNumber = 0;
@@ -318,15 +307,6 @@ namespace ZR.Repository
{
return Context.Ado.SqlQuery<T>(sql, obj);
}
/// <summary>
/// 获得一条数据
/// </summary>
/// <param name="where">Expression<Func<T, bool>></param>
/// <returns></returns>
public T GetFirst(Expression<Func<T, bool>> where)
{
return Context.Queryable<T>().Where(where).First();
}
/// <summary>
/// 根据主值查询单条数据
@@ -371,10 +351,6 @@ namespace ZR.Repository
return Context.Queryable<T>().WithCacheIF(useCache, cacheSecond).ToList();
}
public int Count(Expression<Func<T, bool>> where)
{
return Context.Queryable<T>().Count(where);
}
#endregion query
/// <summary>

View File

@@ -1,44 +1,20 @@
using Infrastructure.Model;
using SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
namespace ZR.Repository
{
public interface IBaseRepository<T> where T : class, new()
public interface IBaseRepository<T> : ISimpleClient<T> where T : class, new()
{
#region add
int Add(T t);
//int Insert(SqlSugarClient client, T t);
int Insert(List<T> t);
int Insert(T parm, Expression<Func<T, object>> iClumns = null, bool ignoreNull = true);
//int InsertIgnoreNullColumn(List<T> t);
//int InsertIgnoreNullColumn(List<T> t, params string[] columns);
//DbResult<bool> InsertTran(T t);
//DbResult<bool> InsertTran(List<T> t);
long InsertReturnBigIdentity(T t);
//T InsertReturnEntity(T t);
//T InsertReturnEntity(T t, string sqlWith = SqlWith.UpdLock);
//bool ExecuteCommand(string sql, object parameters);
//bool ExecuteCommand(string sql, params SugarParameter[] parameters);
//bool ExecuteCommand(string sql, List<SugarParameter> parameters);
IInsertable<T> Insertable(T t);
#endregion add
@@ -70,7 +46,6 @@ namespace ZR.Repository
#region delete
IDeleteable<T> Deleteable();
int Delete(Expression<Func<T, bool>> expression);
int Delete(object[] obj);
int Delete(object id);
int DeleteTable();
@@ -88,24 +63,12 @@ namespace ZR.Repository
PagedInfo<T> GetPages(Expression<Func<T, bool>> where, PagerInfo parm, Expression<Func<T, object>> order, OrderByType orderEnum = OrderByType.Asc);
PagedInfo<T> GetPages(Expression<Func<T, bool>> where, PagerInfo parm, Expression<Func<T, object>> order, string orderByType);
bool Any(Expression<Func<T, bool>> expression);
ISugarQueryable<T> Queryable();
List<T> GetAll(bool useCache = false, int cacheSecond = 3600);
//ISugarQueryable<ExpandoObject> Queryable(string tableName, string shortName);
//ISugarQueryable<T, T1, T2> Queryable<T1, T2>() where T1 : class where T2 : new();
List<T> GetList(Expression<Func<T, bool>> expression);
//Task<List<T>> QueryableToListAsync(Expression<Func<T, bool>> expression);
//string QueryableToJson(string select, Expression<Func<T, bool>> expressionWhere);
//List<T> QueryableToList(string tableName);
(List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, int pageIndex = 0, int pageSize = 10);
(List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, string order, int pageIndex = 0, int pageSize = 10);
@@ -113,23 +76,9 @@ namespace ZR.Repository
(List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, Expression<Func<T, object>> orderFiled, string orderBy, int pageIndex = 0, int pageSize = 10);
List<T> SqlQueryToList(string sql, object obj = null);
/// <summary>
/// 获得一条数据
/// </summary>
/// <param name="where">Expression<Func<T, bool>></param>
/// <returns></returns>
T GetFirst(Expression<Func<T, bool>> where);
T GetId(object pkValue);
/// <summary>
/// 获得一条数据
/// </summary>
/// <param name="parm">string</param>
/// <returns></returns>
//T GetFirst(string parm);
int Count(Expression<Func<T, bool>> where);
#endregion query
#region Procedure