refactor: ai+人工重构优化 framework
This commit is contained in:
@@ -3,10 +3,14 @@ using ArgumentException = System.ArgumentException;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库连接配置选项
|
||||
/// </summary>
|
||||
public class DbConnOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 连接字符串(如果开启多租户,也就是默认库了),必填
|
||||
/// 主数据库连接字符串
|
||||
/// 如果开启多租户,此为默认租户数据库
|
||||
/// </summary>
|
||||
public string? Url { get; set; }
|
||||
|
||||
@@ -16,42 +20,42 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
public DbType? DbType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开启种子数据
|
||||
/// 是否启用种子数据初始化
|
||||
/// </summary>
|
||||
public bool EnabledDbSeed { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 开启驼峰转下划线
|
||||
/// 是否启用驼峰命名转下划线命名
|
||||
/// </summary>
|
||||
public bool EnableUnderLine { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 开启codefirst
|
||||
/// 是否启用Code First模式
|
||||
/// </summary>
|
||||
public bool EnabledCodeFirst { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 开启sql日志
|
||||
/// 是否启用SQL日志记录
|
||||
/// </summary>
|
||||
public bool EnabledSqlLog { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 实体程序集
|
||||
/// 实体类所在程序集名称列表
|
||||
/// </summary>
|
||||
public List<string>? EntityAssembly { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开启读写分离
|
||||
/// 是否启用读写分离
|
||||
/// </summary>
|
||||
public bool EnabledReadWrite { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 读写分离
|
||||
/// 只读数据库连接字符串列表
|
||||
/// </summary>
|
||||
public List<string>? ReadUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开启Saas多租户
|
||||
/// 是否启用SaaS多租户
|
||||
/// </summary>
|
||||
public bool EnabledSaasMultiTenancy { get; set; } = false;
|
||||
}
|
||||
|
||||
@@ -4,10 +4,13 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// 默认租户表特性
|
||||
/// 标记此特性的实体类将在默认租户数据库中创建表
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
||||
public sealed class DefaultTenantTableAttribute : Attribute
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class DefaultTenantTableAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,15 +8,18 @@ using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlSugar数据库上下文接口
|
||||
/// </summary>
|
||||
public interface ISqlSugarDbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlSugarDb
|
||||
/// 获取SqlSugar客户端实例
|
||||
/// </summary>
|
||||
ISqlSugarClient SqlSugarClient { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据库备份
|
||||
/// 执行数据库备份
|
||||
/// </summary>
|
||||
void BackupDataBase();
|
||||
}
|
||||
|
||||
@@ -3,19 +3,55 @@ using SqlSugar;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar数据库上下文依赖接口
|
||||
/// 定义数据库操作的各个生命周期钩子
|
||||
/// </summary>
|
||||
public interface ISqlSugarDbContextDependencies
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行顺序
|
||||
/// 获取执行顺序
|
||||
/// </summary>
|
||||
int ExecutionOrder { get; }
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar客户端配置时触发
|
||||
/// </summary>
|
||||
/// <param name="sqlSugarClient">SqlSugar客户端实例</param>
|
||||
void OnSqlSugarClientConfig(ISqlSugarClient sqlSugarClient);
|
||||
|
||||
/// <summary>
|
||||
/// 数据执行后触发
|
||||
/// </summary>
|
||||
/// <param name="oldValue">原始值</param>
|
||||
/// <param name="entityInfo">实体信息</param>
|
||||
void DataExecuted(object oldValue, DataAfterModel entityInfo);
|
||||
|
||||
/// <summary>
|
||||
/// 数据执行前触发
|
||||
/// </summary>
|
||||
/// <param name="oldValue">原始值</param>
|
||||
/// <param name="entityInfo">实体信息</param>
|
||||
void DataExecuting(object oldValue, DataFilterModel entityInfo);
|
||||
|
||||
void OnLogExecuting(string sql, SugarParameter[] pars);
|
||||
void OnLogExecuted(string sql, SugarParameter[] pars);
|
||||
/// <summary>
|
||||
/// SQL执行前触发
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL语句</param>
|
||||
/// <param name="parameters">SQL参数</param>
|
||||
void OnLogExecuting(string sql, SugarParameter[] parameters);
|
||||
|
||||
/// <summary>
|
||||
/// SQL执行后触发
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL语句</param>
|
||||
/// <param name="parameters">SQL参数</param>
|
||||
void OnLogExecuted(string sql, SugarParameter[] parameters);
|
||||
|
||||
/// <summary>
|
||||
/// 实体服务配置
|
||||
/// </summary>
|
||||
/// <param name="propertyInfo">属性信息</param>
|
||||
/// <param name="entityColumnInfo">实体列信息</param>
|
||||
void EntityService(PropertyInfo propertyInfo, EntityColumnInfo entityColumnInfo);
|
||||
}
|
||||
@@ -6,84 +6,242 @@ using Volo.Abp.Uow;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
|
||||
public interface ISqlSugarRepository<TEntity>:IRepository<TEntity>,IUnitOfWorkEnabled where TEntity : class, IEntity,new ()
|
||||
/// <summary>
|
||||
/// SqlSugar仓储接口
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">实体类型</typeparam>
|
||||
public interface ISqlSugarRepository<TEntity> : IRepository<TEntity>, IUnitOfWorkEnabled
|
||||
where TEntity : class, IEntity, new()
|
||||
{
|
||||
#region 数据库访问器
|
||||
|
||||
/// <summary>
|
||||
/// 获取SqlSugar客户端实例
|
||||
/// </summary>
|
||||
ISqlSugarClient _Db { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询构造器
|
||||
/// </summary>
|
||||
ISugarQueryable<TEntity> _DbQueryable { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取数据库上下文
|
||||
/// </summary>
|
||||
Task<ISqlSugarClient> GetDbContextAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 获取删除操作构造器
|
||||
/// </summary>
|
||||
Task<IDeleteable<TEntity>> AsDeleteable();
|
||||
Task<IInsertable<TEntity>> AsInsertable(List<TEntity> insertObjs);
|
||||
Task<IInsertable<TEntity>> AsInsertable(TEntity insertObj);
|
||||
Task<IInsertable<TEntity>> AsInsertable(TEntity[] insertObjs);
|
||||
|
||||
/// <summary>
|
||||
/// 获取插入操作构造器
|
||||
/// </summary>
|
||||
Task<IInsertable<TEntity>> AsInsertable(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 获取批量插入操作构造器
|
||||
/// </summary>
|
||||
Task<IInsertable<TEntity>> AsInsertable(List<TEntity> entities);
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询构造器
|
||||
/// </summary>
|
||||
Task<ISugarQueryable<TEntity>> AsQueryable();
|
||||
|
||||
/// <summary>
|
||||
/// 获取SqlSugar客户端
|
||||
/// </summary>
|
||||
Task<ISqlSugarClient> AsSugarClient();
|
||||
|
||||
/// <summary>
|
||||
/// 获取租户操作接口
|
||||
/// </summary>
|
||||
Task<ITenant> AsTenant();
|
||||
Task<IUpdateable<TEntity>> AsUpdateable(List<TEntity> updateObjs);
|
||||
Task<IUpdateable<TEntity>> AsUpdateable(TEntity updateObj);
|
||||
|
||||
/// <summary>
|
||||
/// 获取更新操作构造器
|
||||
/// </summary>
|
||||
Task<IUpdateable<TEntity>> AsUpdateable();
|
||||
Task<IUpdateable<TEntity>> AsUpdateable(TEntity[] updateObjs);
|
||||
|
||||
#region 单查
|
||||
//单查
|
||||
/// <summary>
|
||||
/// 获取实体更新操作构造器
|
||||
/// </summary>
|
||||
Task<IUpdateable<TEntity>> AsUpdateable(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 获取批量更新操作构造器
|
||||
/// </summary>
|
||||
Task<IUpdateable<TEntity>> AsUpdateable(List<TEntity> entities);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 查询操作
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键获取实体
|
||||
/// </summary>
|
||||
Task<TEntity> GetByIdAsync(dynamic id);
|
||||
Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> whereExpression);
|
||||
Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression);
|
||||
Task<bool> IsAnyAsync(Expression<Func<TEntity, bool>> whereExpression);
|
||||
Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression);
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 获取满足条件的单个实体
|
||||
/// </summary>
|
||||
Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// 获取满足条件的第一个实体
|
||||
/// </summary>
|
||||
Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
#region 多查
|
||||
//多查
|
||||
/// <summary>
|
||||
/// 判断是否存在满足条件的实体
|
||||
/// </summary>
|
||||
Task<bool> IsAnyAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// 获取满足条件的实体数量
|
||||
/// </summary>
|
||||
Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有实体
|
||||
/// </summary>
|
||||
Task<List<TEntity>> GetListAsync();
|
||||
Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> whereExpression);
|
||||
|
||||
/// <summary>
|
||||
/// 获取满足条件的所有实体
|
||||
/// </summary>
|
||||
Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 分页查询
|
||||
|
||||
/// <summary>
|
||||
/// 获取分页数据
|
||||
/// </summary>
|
||||
Task<List<TEntity>> GetPageListAsync(
|
||||
Expression<Func<TEntity, bool>> predicate,
|
||||
int pageIndex,
|
||||
int pageSize);
|
||||
|
||||
/// <summary>
|
||||
/// 获取排序的分页数据
|
||||
/// </summary>
|
||||
Task<List<TEntity>> GetPageListAsync(
|
||||
Expression<Func<TEntity, bool>> predicate,
|
||||
int pageIndex,
|
||||
int pageSize,
|
||||
Expression<Func<TEntity, object>>? orderByExpression = null,
|
||||
OrderByType orderByType = OrderByType.Asc);
|
||||
|
||||
#region 分页查
|
||||
//分页查
|
||||
Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize);
|
||||
Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<TEntity, object>>? orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
|
||||
#endregion
|
||||
|
||||
#region 插入
|
||||
//插入
|
||||
Task<bool> InsertAsync(TEntity insertObj);
|
||||
Task<bool> InsertOrUpdateAsync(TEntity data);
|
||||
Task<bool> InsertOrUpdateAsync(List<TEntity> datas);
|
||||
Task<int> InsertReturnIdentityAsync(TEntity insertObj);
|
||||
Task<long> InsertReturnBigIdentityAsync(TEntity insertObj);
|
||||
Task<long> InsertReturnSnowflakeIdAsync(TEntity insertObj);
|
||||
Task<TEntity> InsertReturnEntityAsync(TEntity insertObj);
|
||||
Task<bool> InsertRangeAsync(List<TEntity> insertObjs);
|
||||
#region 插入操作
|
||||
|
||||
/// <summary>
|
||||
/// 插入实体
|
||||
/// </summary>
|
||||
Task<bool> InsertAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 插入或更新实体
|
||||
/// </summary>
|
||||
Task<bool> InsertOrUpdateAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入或更新实体
|
||||
/// </summary>
|
||||
Task<bool> InsertOrUpdateAsync(List<TEntity> entities);
|
||||
|
||||
/// <summary>
|
||||
/// 插入实体并返回自增主键
|
||||
/// </summary>
|
||||
Task<int> InsertReturnIdentityAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 插入实体并返回长整型自增主键
|
||||
/// </summary>
|
||||
Task<long> InsertReturnBigIdentityAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 插入实体并返回雪花ID
|
||||
/// </summary>
|
||||
Task<long> InsertReturnSnowflakeIdAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 插入实体并返回实体
|
||||
/// </summary>
|
||||
Task<TEntity> InsertReturnEntityAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入实体
|
||||
/// </summary>
|
||||
Task<bool> InsertRangeAsync(List<TEntity> entities);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 更新操作
|
||||
|
||||
/// <summary>
|
||||
/// 更新实体
|
||||
/// </summary>
|
||||
Task<bool> UpdateAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新实体
|
||||
/// </summary>
|
||||
Task<bool> UpdateRangeAsync(List<TEntity> entities);
|
||||
|
||||
/// <summary>
|
||||
/// 条件更新指定列
|
||||
/// </summary>
|
||||
Task<bool> UpdateAsync(
|
||||
Expression<Func<TEntity, TEntity>> columns,
|
||||
Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
#region 更新
|
||||
//更新
|
||||
Task<bool> UpdateAsync(TEntity updateObj);
|
||||
Task<bool> UpdateRangeAsync(List<TEntity> updateObjs);
|
||||
Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression);
|
||||
#endregion
|
||||
|
||||
#region 删除
|
||||
//删除
|
||||
Task<bool> DeleteAsync(TEntity deleteObj);
|
||||
Task<bool> DeleteAsync(List<TEntity> deleteObjs);
|
||||
Task<bool> DeleteAsync(Expression<Func<TEntity, bool>> whereExpression);
|
||||
#region 删除操作
|
||||
|
||||
/// <summary>
|
||||
/// 删除实体
|
||||
/// </summary>
|
||||
Task<bool> DeleteAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除实体
|
||||
/// </summary>
|
||||
Task<bool> DeleteAsync(List<TEntity> entities);
|
||||
|
||||
/// <summary>
|
||||
/// 条件删除
|
||||
/// </summary>
|
||||
Task<bool> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键删除
|
||||
/// </summary>
|
||||
Task<bool> DeleteByIdAsync(dynamic id);
|
||||
Task<bool> DeleteByIdsAsync(dynamic[] ids);
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键批量删除
|
||||
/// </summary>
|
||||
Task<bool> DeleteByIdsAsync(dynamic[] ids);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
public interface ISqlSugarRepository<TEntity, TKey> : ISqlSugarRepository<TEntity>,IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar仓储接口(带主键)
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">实体类型</typeparam>
|
||||
/// <typeparam name="TKey">主键类型</typeparam>
|
||||
public interface ISqlSugarRepository<TEntity, TKey> :
|
||||
ISqlSugarRepository<TEntity>,
|
||||
IRepository<TEntity, TKey>
|
||||
where TEntity : class, IEntity<TKey>, new()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,17 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlSugar数据库上下文提供者接口
|
||||
/// </summary>
|
||||
/// <typeparam name="TDbContext">数据库上下文类型</typeparam>
|
||||
public interface ISugarDbContextProvider<TDbContext>
|
||||
where TDbContext : ISqlSugarDbContext
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取数据库上下文实例
|
||||
/// </summary>
|
||||
/// <returns>数据库上下文实例</returns>
|
||||
Task<TDbContext> GetDbContextAsync();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
/// <summary>
|
||||
/// 忽略CodeFirst特性
|
||||
/// 标记此特性的实体类将不会被CodeFirst功能扫描
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class IgnoreCodeFirstAttribute : Attribute
|
||||
{
|
||||
|
||||
@@ -3,9 +3,13 @@ using Yi.Framework.Core;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore.Abstractions
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlSugar Core抽象层模块
|
||||
/// 提供SqlSugar ORM的基础抽象接口和类型定义
|
||||
/// </summary>
|
||||
[DependsOn(typeof(YiFrameworkCoreModule))]
|
||||
public class YiFrameworkSqlSugarCoreAbstractionsModule : AbpModule
|
||||
{
|
||||
|
||||
// 模块配置方法可在此添加
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user