@@ -15,14 +15,15 @@ using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.SqlSugarCore.Repositories
{
public class SqlSugarRepository < TEntity > : ISqlSugarRepository < TEntity > , IRepository < TEntity > where TEntity : class , IEntity , new ( )
public class SqlSugarRepository < TEntity > : ISqlSugarRepository < TEntity > , IRepository < TEntity >
where TEntity : class , IEntity , new ( )
{
public ISqlSugarClient _Db = > AsyncContext . Run ( async ( ) = > await GetDbContextAsync ( ) ) ;
public ISugarQueryable < TEntity > _DbQueryable = > _Db . Queryable < TEntity > ( ) ;
private readonly ISugarDbContextProvider < ISqlSugarDbContext > _dbContextProvider ;
/// <summary>
/// 异步查询执行器
/// </summary>
@@ -58,22 +59,26 @@ namespace Yi.Framework.SqlSugarCore.Repositories
#region Abp模块
public virtual async Task < TEntity ? > FindAsync ( Expression < Func < TEntity , bool > > predicate , bool includeDetails = true , CancellationToken cancellationToken = default )
public virtual async Task < TEntity ? > FindAsync ( Expression < Func < TEntity , bool > > predicate ,
bool includeDetails = true , CancellationToken cancellationToken = default )
{
return await GetFirstAsync ( predicate ) ;
}
public virtual async Task < TEntity > GetAsync ( Expression < Func < TEntity , bool > > predicate , bool includeDetails = true , CancellationToken cancellationToken = default )
public virtual async Task < TEntity > GetAsync ( Expression < Func < TEntity , bool > > predicate ,
bool includeDetails = true , CancellationToken cancellationToken = default )
{
return await GetFirstAsync ( predicate ) ;
}
public virtual async Task DeleteAsync ( Expression < Func < TEntity , bool > > predicate , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task DeleteAsync ( Expression < Func < TEntity , bool > > predicate , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await this . DeleteAsync ( predicate ) ;
}
public virtual async Task DeleteDirectAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default )
public virtual async Task DeleteDirectAsync ( Expression < Func < TEntity , bool > > predicate ,
CancellationToken cancellationToken = default )
{
await this . DeleteAsync ( predicate ) ;
}
@@ -103,60 +108,71 @@ namespace Yi.Framework.SqlSugarCore.Repositories
throw new NotImplementedException ( ) ;
}
public virtual async Task < List < TEntity > > GetListAsync ( Expression < Func < TEntity , bool > > predicate , bool includeDetails = false , CancellationToken cancellationToken = default )
public virtual async Task < List < TEntity > > GetListAsync ( Expression < Func < TEntity , bool > > predicate ,
bool includeDetails = false , CancellationToken cancellationToken = default )
{
return await GetListAsync ( predicate ) ;
}
public virtual async Task < TEntity > InsertAsync ( TEntity entity , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task < TEntity > InsertAsync ( TEntity entity , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
return await InsertReturnEntityAsync ( entity ) ;
}
public virtual async Task InsertManyAsync ( IEnumerable < TEntity > entities , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task InsertManyAsync ( IEnumerable < TEntity > entities , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await InsertRangeAsync ( entities . ToList ( ) ) ;
}
public virtual async Task < TEntity > UpdateAsync ( TEntity entity , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task < TEntity > UpdateAsync ( TEntity entity , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await UpdateAsync ( entity ) ;
return entity ;
}
public virtual async Task UpdateManyAsync ( IEnumerable < TEntity > entities , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task UpdateManyAsync ( IEnumerable < TEntity > entities , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await UpdateRangeAsync ( entities . ToList ( ) ) ;
}
public virtual async Task DeleteAsync ( TEntity entity , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task DeleteAsync ( TEntity entity , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await DeleteAsync ( entity ) ;
}
public virtual async Task DeleteManyAsync ( IEnumerable < TEntity > entities , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task DeleteManyAsync ( IEnumerable < TEntity > entities , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await DeleteAsync ( entities . ToList ( ) ) ;
}
public virtual async Task < List < TEntity > > GetListAsync ( bool includeDetails = false , CancellationToken cancellationToken = default )
public virtual async Task < List < TEntity > > GetListAsync ( bool includeDetails = false ,
CancellationToken cancellationToken = default )
{
return await GetListAsync ( ) ;
}
public virtual async Task < long > GetCountAsync ( CancellationToken cancellationToken = default )
{
return await this . CountAsync ( _ = > true ) ;
return await this . CountAsync ( _ = > true ) ;
}
public virtual async Task < List < TEntity > > GetPagedListAsync ( int skipCount , int maxResultCount , string sorting , bool includeDetails = false , CancellationToken cancellationToken = default )
public virtual async Task < List < TEntity > > GetPagedListAsync ( int skipCount , int maxResultCount , string sorting ,
bool includeDetails = false , CancellationToken cancellationToken = default )
{
return await GetPageListAsync ( _ = > true , skipCount , maxResultCount ) ;
}
#endregion
#region 内 置 DB快捷操作
public virtual async Task < IDeleteable < TEntity > > AsDeleteable ( )
{
return ( await GetDbSimpleClientAsync ( ) ) . AsDeleteable ( ) ;
@@ -171,7 +187,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{
return ( await GetDbSimpleClientAsync ( ) ) . AsInsertable ( insertObj ) ;
}
public virtual async Task < IInsertable < TEntity > > AsInsertable ( TEntity [ ] insertObjs )
{
return ( await GetDbSimpleClientAsync ( ) ) . AsInsertable ( insertObjs ) ;
@@ -211,9 +227,11 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{
return ( await GetDbSimpleClientAsync ( ) ) . AsUpdateable ( updateObjs ) ;
}
#endregion
#region SimpleClient模块
public virtual async Task < int > CountAsync ( Expression < Func < TEntity , bool > > whereExpression )
{
return await ( await GetDbSimpleClientAsync ( ) ) . CountAsync ( whereExpression ) ;
@@ -230,7 +248,6 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{
return await ( await GetDbSimpleClientAsync ( ) ) . DeleteAsync ( deleteObj ) ;
}
}
public virtual async Task < bool > DeleteAsync ( List < TEntity > deleteObjs )
@@ -250,13 +267,13 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{
if ( typeof ( ISoftDelete ) . IsAssignableFrom ( typeof ( TEntity ) ) )
{
return await ( await GetDbSimpleClientAsync ( ) ) . AsUpdateable ( ) . SetColumns ( nameof ( ISoftDelete . IsDeleted ) , true ) . Where ( whereExpression ) . ExecuteCommandAsync ( ) > 0 ;
return await ( await GetDbSimpleClientAsync ( ) ) . AsUpdateable ( )
. SetColumns ( nameof ( ISoftDelete . IsDeleted ) , true ) . Where ( whereExpression ) . ExecuteCommandAsync ( ) > 0 ;
}
else
{
return await ( await GetDbSimpleClientAsync ( ) ) . DeleteAsync ( whereExpression ) ;
}
}
public virtual async Task < bool > DeleteByIdAsync ( dynamic id )
@@ -264,6 +281,11 @@ namespace Yi.Framework.SqlSugarCore.Repositories
if ( typeof ( ISoftDelete ) . IsAssignableFrom ( typeof ( TEntity ) ) )
{
var entity = await GetByIdAsync ( id ) ;
if ( entity is null )
{
return false ;
}
//反射赋值
ReflexHelper . SetModelValue ( nameof ( ISoftDelete . IsDeleted ) , true , entity ) ;
return await UpdateAsync ( entity ) ;
@@ -284,6 +306,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{
return false ;
}
//反射赋值
entities . ForEach ( e = > ReflexHelper . SetModelValue ( nameof ( ISoftDelete . IsDeleted ) , true , e ) ) ;
return await UpdateRangeAsync ( entities ) ;
@@ -292,7 +315,6 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{
return await ( await GetDbSimpleClientAsync ( ) ) . DeleteByIdAsync ( ids ) ;
}
}
public virtual async Task < TEntity > GetByIdAsync ( dynamic id )
@@ -301,7 +323,6 @@ namespace Yi.Framework.SqlSugarCore.Repositories
}
public virtual async Task < TEntity > GetFirstAsync ( Expression < Func < TEntity , bool > > whereExpression )
{
return await ( await GetDbSimpleClientAsync ( ) ) . GetFirstAsync ( whereExpression ) ;
@@ -317,14 +338,19 @@ namespace Yi.Framework.SqlSugarCore.Repositories
return await ( await GetDbSimpleClientAsync ( ) ) . GetListAsync ( whereExpression ) ;
}
public virtual async Task < List < TEntity > > GetPageListAsync ( Expression < Func < TEntity , bool > > whereExpression , int pageNum , int pageSize )
public virtual async Task < List < TEntity > > GetPageListAsync ( Expression < Func < TEntity , bool > > whereExpression ,
int pageNum , int pageSize )
{
return await ( await GetDbSimpleClientAsync ( ) ) . GetPageListAsync ( whereExpression , new PageModel ( ) { PageIndex = pageNum , PageSize = pageSize } ) ;
return await ( await GetDbSimpleClientAsync ( ) ) . GetPageListAsync ( whereExpression ,
new PageModel ( ) { PageIndex = pageNum , PageSize = pageSize } ) ;
}
public virtual async Task < List < TEntity > > GetPageListAsync ( Expression < Func < TEntity , bool > > whereExpression , int pageNum , int pageSize , Expression < Func < TEntity , object > > ? orderByExpression = null , OrderByType orderByType = OrderByType . Asc )
public virtual async Task < List < TEntity > > GetPageListAsync ( Expression < Func < TEntity , bool > > whereExpression ,
int pageNum , int pageSize , Expression < Func < TEntity , object > > ? orderByExpression = null ,
OrderByType orderByType = OrderByType . Asc )
{
return await ( await GetDbSimpleClientAsync ( ) ) . GetPageListAsync ( whereExpression , new PageModel { PageIndex = pageNum , PageSize = pageSize } , orderByExpression , orderByType ) ;
return await ( await GetDbSimpleClientAsync ( ) ) . GetPageListAsync ( whereExpression ,
new PageModel { PageIndex = pageNum , PageSize = pageSize } , orderByExpression , orderByType ) ;
}
public virtual async Task < TEntity > GetSingleAsync ( Expression < Func < TEntity , bool > > whereExpression )
@@ -379,30 +405,31 @@ namespace Yi.Framework.SqlSugarCore.Repositories
public virtual async Task < bool > UpdateAsync ( TEntity updateObj )
{
if ( typeof ( TEntity ) . IsAssignableTo < IHasConcurrencyStamp > ( ) ) //带版本号乐观锁更新
if ( typeof ( TEntity ) . IsAssignableTo < IHasConcurrencyStamp > ( ) ) //带版本号乐观锁更新
{
try
{
int num = await ( await GetDbSimpleClientAsync ( ) )
int num = await ( await GetDbSimpleClientAsync ( ) )
. Context . Updateable ( updateObj ) . ExecuteCommandWithOptLockAsync ( true ) ;
return num > 0 ;
return num > 0 ;
}
catch ( VersionExceptions ex )
{
throw new AbpDbConcurrencyException ( $"{ex.Message}[更新失败: ConcurrencyStamp不是最新版本],entityInfo: {updateObj}" , ex ) ;
throw new AbpDbConcurrencyException (
$"{ex.Message}[更新失败: ConcurrencyStamp不是最新版本],entityInfo: {updateObj}" , ex ) ;
}
}
return await ( await GetDbSimpleClientAsync ( ) ) . UpdateAsync ( updateObj ) ;
}
public virtual async Task < bool > UpdateAsync ( Expression < Func < TEntity , TEntity > > columns , Expression < Func < TEntity , bool > > whereExpression )
public virtual async Task < bool > UpdateAsync ( Expression < Func < TEntity , TEntity > > columns ,
Expression < Func < TEntity , bool > > whereExpression )
{
return await ( await GetDbSimpleClientAsync ( ) ) . UpdateAsync ( columns , whereExpression ) ;
}
public virtual async Task < bool > UpdateRangeAsync ( List < TEntity > updateObjs )
{
return await ( await GetDbSimpleClientAsync ( ) ) . UpdateRangeAsync ( updateObjs ) ;
@@ -411,30 +438,36 @@ namespace Yi.Framework.SqlSugarCore.Repositories
#endregion
}
public class SqlSugarRepository < TEntity , TKey > : SqlSugarRepository < TEntity > , ISqlSugarRepository < TEntity , TKey > , IRepository < TEntity , TKey > where TEntity : class , IEntity < TKey > , new ( )
public class SqlSugarRepository < TEntity , TKey > : SqlSugarRepository < TEntity > , ISqlSugarRepository < TEntity , TKey > ,
IRepository < TEntity , TKey > where TEntity : class , IEntity < TKey > , new ( )
{
public SqlSugarRepository ( ISugarDbContextProvider < ISqlSugarDbContext > sugarDbContextProvider ) : base ( sugarDbContextProvider )
public SqlSugarRepository ( ISugarDbContextProvider < ISqlSugarDbContext > sugarDbContextProvider ) : base (
sugarDbContextProvider )
{
}
public virtual async Task DeleteAsync ( TKey id , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task DeleteAsync ( TKey id , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await DeleteByIdAsync ( id ) ;
}
public virtual async Task DeleteManyAsync ( IEnumerable < TKey > ids , bool autoSave = false , CancellationToken cancellationToken = default )
public virtual async Task DeleteManyAsync ( IEnumerable < TKey > ids , bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await DeleteByIdsAsync ( ids . Select ( x = > ( object ) x ) . ToArray ( ) ) ;
}
public virtual async Task < TEntity ? > FindAsync ( TKey id , bool includeDetails = true , CancellationToken cancellationToken = default )
public virtual async Task < TEntity ? > FindAsync ( TKey id , bool includeDetails = true ,
CancellationToken cancellationToken = default )
{
return await GetByIdAsync ( id ) ;
}
public virtual async Task < TEntity > GetAsync ( TKey id , bool includeDetails = true , CancellationToken cancellationToken = default )
public virtual async Task < TEntity > GetAsync ( TKey id , bool includeDetails = true ,
CancellationToken cancellationToken = default )
{
return await GetByIdAsync ( id ) ;
}
}
}
}