feat: 完成多db功能搭建
This commit is contained in:
@@ -0,0 +1,207 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Auditing;
|
||||||
|
using Volo.Abp.Data;
|
||||||
|
using Volo.Abp.DependencyInjection;
|
||||||
|
using Volo.Abp.Domain.Entities;
|
||||||
|
using Volo.Abp.Domain.Entities.Events;
|
||||||
|
using Volo.Abp.Guids;
|
||||||
|
using Volo.Abp.MultiTenancy;
|
||||||
|
using Volo.Abp.Users;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.SqlSugarCore;
|
||||||
|
|
||||||
|
public class DefaultSqlSugarDbContext : SqlSugarDbContext
|
||||||
|
{
|
||||||
|
|
||||||
|
protected DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
|
||||||
|
protected ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService<ICurrentUser>();
|
||||||
|
protected IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetRequiredService<IGuidGenerator>();
|
||||||
|
protected ILoggerFactory Logger => LazyServiceProvider.LazyGetRequiredService<ILoggerFactory>();
|
||||||
|
protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
|
||||||
|
protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService<IDataFilter>();
|
||||||
|
protected virtual bool IsMultiTenantFilterEnabled => DataFilter?.IsEnabled<IMultiTenant>() ?? false;
|
||||||
|
protected virtual bool IsSoftDeleteFilterEnabled => DataFilter?.IsEnabled<ISoftDelete>() ?? false;
|
||||||
|
|
||||||
|
protected IEntityChangeEventHelper EntityChangeEventHelper =>
|
||||||
|
LazyServiceProvider.LazyGetService<IEntityChangeEventHelper>(NullEntityChangeEventHelper.Instance);
|
||||||
|
|
||||||
|
protected override void CustomDataFilter(ISqlSugarClient sqlSugarClient)
|
||||||
|
{
|
||||||
|
if (IsSoftDeleteFilterEnabled)
|
||||||
|
{
|
||||||
|
sqlSugarClient.QueryFilter.AddTableFilter<ISoftDelete>(u => u.IsDeleted == false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsMultiTenantFilterEnabled)
|
||||||
|
{
|
||||||
|
//表达式里只能有具体值,不能运算
|
||||||
|
var expressionCurrentTenant = CurrentTenant.Id ?? null;
|
||||||
|
sqlSugarClient.QueryFilter.AddTableFilter<IMultiTenant>(u => u.TenantId == expressionCurrentTenant);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void DataExecuting(object oldValue, DataFilterModel entityInfo)
|
||||||
|
{
|
||||||
|
//审计日志
|
||||||
|
switch (entityInfo.OperationType)
|
||||||
|
{
|
||||||
|
case DataFilterType.UpdateByObject:
|
||||||
|
|
||||||
|
if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModificationTime)))
|
||||||
|
{
|
||||||
|
if (!DateTime.MinValue.Equals(oldValue))
|
||||||
|
{
|
||||||
|
entityInfo.SetValue(DateTime.Now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModifierId)))
|
||||||
|
{
|
||||||
|
if (typeof(Guid?) == entityInfo.EntityColumnInfo.PropertyInfo.PropertyType)
|
||||||
|
{
|
||||||
|
if (CurrentUser.Id != null)
|
||||||
|
{
|
||||||
|
entityInfo.SetValue(CurrentUser.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case DataFilterType.InsertByObject:
|
||||||
|
|
||||||
|
if (entityInfo.PropertyName.Equals(nameof(IEntity<Guid>.Id)))
|
||||||
|
{
|
||||||
|
//类型为guid
|
||||||
|
if (typeof(Guid) == entityInfo.EntityColumnInfo.PropertyInfo.PropertyType)
|
||||||
|
{
|
||||||
|
//主键为空或者为默认最小值
|
||||||
|
if (Guid.Empty.Equals(oldValue))
|
||||||
|
{
|
||||||
|
entityInfo.SetValue(GuidGenerator.Create());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreationTime)))
|
||||||
|
{
|
||||||
|
//为空或者为默认最小值
|
||||||
|
if (DateTime.MinValue.Equals(oldValue))
|
||||||
|
{
|
||||||
|
entityInfo.SetValue(DateTime.Now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreatorId)))
|
||||||
|
{
|
||||||
|
//类型为guid
|
||||||
|
if (typeof(Guid?) == entityInfo.EntityColumnInfo.PropertyInfo.PropertyType)
|
||||||
|
{
|
||||||
|
if (CurrentUser.Id is not null)
|
||||||
|
{
|
||||||
|
entityInfo.SetValue(CurrentUser.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (entityInfo.PropertyName.Equals(nameof(IMultiTenant.TenantId)))
|
||||||
|
{
|
||||||
|
if (CurrentTenant.Id is not null)
|
||||||
|
{
|
||||||
|
entityInfo.SetValue(CurrentTenant.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//领域事件
|
||||||
|
switch (entityInfo.OperationType)
|
||||||
|
{
|
||||||
|
case DataFilterType.InsertByObject:
|
||||||
|
if (entityInfo.PropertyName == nameof(IEntity<object>.Id))
|
||||||
|
{
|
||||||
|
EntityChangeEventHelper.PublishEntityCreatedEvent(entityInfo.EntityValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case DataFilterType.UpdateByObject:
|
||||||
|
if (entityInfo.PropertyName == nameof(IEntity<object>.Id))
|
||||||
|
{
|
||||||
|
//软删除,发布的是删除事件
|
||||||
|
if (entityInfo.EntityValue is ISoftDelete softDelete)
|
||||||
|
{
|
||||||
|
if (softDelete.IsDeleted == true)
|
||||||
|
{
|
||||||
|
EntityChangeEventHelper.PublishEntityDeletedEvent(entityInfo.EntityValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EntityChangeEventHelper.PublishEntityUpdatedEvent(entityInfo.EntityValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case DataFilterType.DeleteByObject:
|
||||||
|
if (entityInfo.PropertyName == nameof(IEntity<object>.Id))
|
||||||
|
{
|
||||||
|
//这里sqlsugar有个特殊,删除会返回批量的结果
|
||||||
|
if (entityInfo.EntityValue is IEnumerable entityValues)
|
||||||
|
{
|
||||||
|
foreach (var entityValue in entityValues)
|
||||||
|
{
|
||||||
|
EntityChangeEventHelper.PublishEntityDeletedEvent(entityValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnLogExecuting(string sql, SugarParameter[] pars)
|
||||||
|
{
|
||||||
|
if (Options.EnabledSqlLog)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.AppendLine();
|
||||||
|
sb.AppendLine("==========Yi-SQL执行:==========");
|
||||||
|
sb.AppendLine(UtilMethods.GetSqlString(DbType.SqlServer, sql, pars));
|
||||||
|
sb.AppendLine("===============================");
|
||||||
|
Logger.CreateLogger<DefaultSqlSugarDbContext>().LogDebug(sb.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnLogExecuted(string sql, SugarParameter[] pars)
|
||||||
|
{
|
||||||
|
if (Options.EnabledSqlLog)
|
||||||
|
{
|
||||||
|
var sqllog = $"=========Yi-SQL耗时{SqlSugarClient.Ado.SqlExecutionTime.TotalMilliseconds}毫秒=====";
|
||||||
|
Logger.CreateLogger<SqlSugarDbContext>().LogDebug(sqllog.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void EntityService(PropertyInfo propertyInfo, EntityColumnInfo entityColumnInfo)
|
||||||
|
{
|
||||||
|
if (propertyInfo.Name == nameof(IHasConcurrencyStamp.ConcurrencyStamp)) //带版本号并发更新
|
||||||
|
{
|
||||||
|
entityColumnInfo.IsEnableUpdateVersionValidation = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (propertyInfo.PropertyType == typeof(ExtraPropertyDictionary))
|
||||||
|
{
|
||||||
|
entityColumnInfo.IsIgnore = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (propertyInfo.Name == nameof(Entity<object>.Id))
|
||||||
|
{
|
||||||
|
entityColumnInfo.IsPrimarykey = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,13 +5,12 @@ namespace Yi.Framework.SqlSugarCore;
|
|||||||
|
|
||||||
public interface ISqlSugarDbContextDependencies
|
public interface ISqlSugarDbContextDependencies
|
||||||
{
|
{
|
||||||
void OnSqlSugarClientConfig(ISqlSugarClient client);
|
void OnSqlSugarClientConfig(ISqlSugarClient sqlSugarClient);
|
||||||
|
void DataExecuted(object oldValue, DataAfterModel entityInfo);
|
||||||
|
void DataExecuting(object oldValue, DataFilterModel entityInfo);
|
||||||
|
|
||||||
void DataExecuted(object obj, DataAfterModel dataAfterModel);
|
void OnLogExecuting(string sql, SugarParameter[] pars);
|
||||||
void DataExecuting(object obj, DataFilterModel dataAfterModel);
|
void OnLogExecuted(string sql, SugarParameter[] pars);
|
||||||
|
|
||||||
void OnLogExecuting(string str, SugarParameter[] parameters);
|
|
||||||
void OnLogExecuted(string str, SugarParameter[] parameters);
|
|
||||||
|
|
||||||
void EntityService(PropertyInfo propertyInfo, EntityColumnInfo entityColumnInfo);
|
void EntityService(PropertyInfo propertyInfo, EntityColumnInfo entityColumnInfo);
|
||||||
}
|
}
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
using System.Reflection;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using SqlSugar;
|
|
||||||
using Volo.Abp.Data;
|
|
||||||
using Volo.Abp.DependencyInjection;
|
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
|
||||||
|
|
||||||
namespace Yi.Framework.SqlSugarCore
|
|
||||||
{
|
|
||||||
|
|
||||||
public class SqlSugarDbConnectionCreator: ISqlSugarDbConnectionCreator,ITransientDependency
|
|
||||||
{
|
|
||||||
public SqlSugarDbConnectionCreator(IOptions<DbConnOptions> options)
|
|
||||||
{
|
|
||||||
Options = options.Value;
|
|
||||||
}
|
|
||||||
public DbConnOptions Options { get; }
|
|
||||||
|
|
||||||
public void SetDbAop(ISqlSugarClient currentDb)
|
|
||||||
{
|
|
||||||
currentDb.Aop.OnLogExecuting = this.OnLogExecuting;
|
|
||||||
currentDb.Aop.OnLogExecuted = this.OnLogExecuted;
|
|
||||||
currentDb.Aop.DataExecuting = this.DataExecuting;
|
|
||||||
currentDb.Aop.DataExecuted = this.DataExecuted;
|
|
||||||
OnSqlSugarClientConfig(currentDb);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public ConnectionConfig Build(Action<ConnectionConfig>? action=null)
|
|
||||||
{
|
|
||||||
var dbConnOptions = Options;
|
|
||||||
#region 组装options
|
|
||||||
if (dbConnOptions.DbType is null)
|
|
||||||
{
|
|
||||||
throw new ArgumentException("DbType配置为空");
|
|
||||||
}
|
|
||||||
var slavaConFig = new List<SlaveConnectionConfig>();
|
|
||||||
if (dbConnOptions.EnabledReadWrite)
|
|
||||||
{
|
|
||||||
if (dbConnOptions.ReadUrl is null)
|
|
||||||
{
|
|
||||||
throw new ArgumentException("读写分离为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
var readCon = dbConnOptions.ReadUrl;
|
|
||||||
|
|
||||||
readCon.ForEach(s =>
|
|
||||||
{
|
|
||||||
//如果是动态saas分库,这里的连接串都不能写死,需要动态添加,这里只配置共享库的连接
|
|
||||||
slavaConFig.Add(new SlaveConnectionConfig() { ConnectionString = s });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region 组装连接config
|
|
||||||
var connectionConfig = new ConnectionConfig()
|
|
||||||
{
|
|
||||||
ConfigId= ConnectionStrings.DefaultConnectionStringName,
|
|
||||||
DbType = dbConnOptions.DbType ?? DbType.Sqlite,
|
|
||||||
ConnectionString = dbConnOptions.Url,
|
|
||||||
IsAutoCloseConnection = true,
|
|
||||||
SlaveConnectionConfigs = slavaConFig,
|
|
||||||
//设置codefirst非空值判断
|
|
||||||
ConfigureExternalServices = new ConfigureExternalServices
|
|
||||||
{
|
|
||||||
// 处理表
|
|
||||||
EntityNameService = (type, entity) =>
|
|
||||||
{
|
|
||||||
if (dbConnOptions.EnableUnderLine && !entity.DbTableName.Contains('_'))
|
|
||||||
entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName);// 驼峰转下划线
|
|
||||||
},
|
|
||||||
EntityService = (c, p) =>
|
|
||||||
{
|
|
||||||
if (new NullabilityInfoContext()
|
|
||||||
.Create(c).WriteState is NullabilityState.Nullable)
|
|
||||||
{
|
|
||||||
p.IsNullable = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dbConnOptions.EnableUnderLine && !p.IsIgnore && !p.DbColumnName.Contains('_'))
|
|
||||||
p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName);// 驼峰转下划线
|
|
||||||
|
|
||||||
EntityService(c, p);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
//这里多租户有个坑,无效的
|
|
||||||
AopEvents = new AopEvents
|
|
||||||
{
|
|
||||||
DataExecuted = DataExecuted,
|
|
||||||
DataExecuting = DataExecuting,
|
|
||||||
OnLogExecuted = OnLogExecuted,
|
|
||||||
OnLogExecuting = OnLogExecuting
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
if (action is not null)
|
|
||||||
{
|
|
||||||
action.Invoke(connectionConfig);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
return connectionConfig;
|
|
||||||
}
|
|
||||||
[DisablePropertyInjection]
|
|
||||||
public Action<ISqlSugarClient> OnSqlSugarClientConfig { get; set; }
|
|
||||||
|
|
||||||
[DisablePropertyInjection]
|
|
||||||
public Action<object, DataAfterModel> DataExecuted { get; set; }
|
|
||||||
|
|
||||||
[DisablePropertyInjection]
|
|
||||||
public Action<object, DataFilterModel> DataExecuting { get; set; }
|
|
||||||
|
|
||||||
[DisablePropertyInjection]
|
|
||||||
public Action<string, SugarParameter[]> OnLogExecuting { get; set; }
|
|
||||||
|
|
||||||
[DisablePropertyInjection]
|
|
||||||
public Action<string, SugarParameter[]> OnLogExecuted { get; set; }
|
|
||||||
|
|
||||||
[DisablePropertyInjection]
|
|
||||||
public Action<PropertyInfo, EntityColumnInfo> EntityService { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Yi.Framework.SqlSugarCore;
|
||||||
|
|
||||||
|
public abstract class SqlSugarDbContext : ISqlSugarDbContextDependencies
|
||||||
|
{
|
||||||
|
//属性注入
|
||||||
|
public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
|
||||||
|
protected ISqlSugarClient SqlSugarClient { get;private set; }
|
||||||
|
public void OnSqlSugarClientConfig(ISqlSugarClient sqlSugarClient)
|
||||||
|
{
|
||||||
|
SqlSugarClient = sqlSugarClient;
|
||||||
|
CustomDataFilter(sqlSugarClient);
|
||||||
|
}
|
||||||
|
protected virtual void CustomDataFilter(ISqlSugarClient sqlSugarClient)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void DataExecuted(object oldValue, DataAfterModel entityInfo)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void DataExecuting(object oldValue, DataFilterModel entityInfo)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnLogExecuting(string sql, SugarParameter[] pars)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnLogExecuted(string sql, SugarParameter[] pars)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void EntityService(PropertyInfo propertyInfo, EntityColumnInfo entityColumnInfo)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,13 +17,9 @@ namespace Yi.Framework.SqlSugarCore
|
|||||||
/// SqlSugar 客户端
|
/// SqlSugar 客户端
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ISqlSugarClient SqlSugarClient { get; private set; }
|
public ISqlSugarClient SqlSugarClient { get; private set; }
|
||||||
|
|
||||||
protected ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService<ICurrentUser>();
|
|
||||||
private IAbpLazyServiceProvider LazyServiceProvider { get; }
|
private IAbpLazyServiceProvider LazyServiceProvider { get; }
|
||||||
|
|
||||||
private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
|
private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
|
||||||
protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService<IDataFilter>();
|
|
||||||
|
|
||||||
public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
|
public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
|
||||||
|
|
||||||
private ISerializeService SerializeService => LazyServiceProvider.LazyGetRequiredService<ISerializeService>();
|
private ISerializeService SerializeService => LazyServiceProvider.LazyGetRequiredService<ISerializeService>();
|
||||||
@@ -34,18 +30,17 @@ namespace Yi.Framework.SqlSugarCore
|
|||||||
{
|
{
|
||||||
LazyServiceProvider = lazyServiceProvider;
|
LazyServiceProvider = lazyServiceProvider;
|
||||||
|
|
||||||
//获取连接配置
|
//获取连接配置操作,需要进行缓存
|
||||||
var connectionConfig = BuildConnectionConfig(action: options =>
|
var connectionConfig = BuildConnectionConfig(action: options =>
|
||||||
{
|
{
|
||||||
options.ConnectionString = GetCurrentConnectionString();
|
options.ConnectionString = GetCurrentConnectionString();
|
||||||
options.DbType = GetCurrentDbType();
|
options.DbType = GetCurrentDbType();
|
||||||
});
|
});
|
||||||
SqlSugarClient = new SqlSugarClient(connectionConfig);
|
SqlSugarClient = new SqlSugarClient(connectionConfig);
|
||||||
|
//生命周期,以下都可以直接使用sqlsugardb了
|
||||||
//替换默认序列化器
|
|
||||||
SqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SerializeService = SerializeService;
|
|
||||||
|
|
||||||
// Aop及多租户连接字符串和类型,需要单独设置
|
// Aop及多租户连接字符串和类型,需要单独设置
|
||||||
|
// Aop操作需要进行缓存
|
||||||
SetDbAop(SqlSugarClient);
|
SetDbAop(SqlSugarClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +50,9 @@ namespace Yi.Framework.SqlSugarCore
|
|||||||
/// <param name="sqlSugarClient"></param>
|
/// <param name="sqlSugarClient"></param>
|
||||||
protected virtual void SetDbAop(ISqlSugarClient sqlSugarClient)
|
protected virtual void SetDbAop(ISqlSugarClient sqlSugarClient)
|
||||||
{
|
{
|
||||||
|
//替换默认序列化器
|
||||||
|
sqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SerializeService = SerializeService;
|
||||||
|
|
||||||
//将所有,ISqlSugarDbContextDependencies进行累加
|
//将所有,ISqlSugarDbContextDependencies进行累加
|
||||||
Action<string, SugarParameter[]> onLogExecuting=null;
|
Action<string, SugarParameter[]> onLogExecuting=null;
|
||||||
Action<string, SugarParameter[]> onLogExecuted=null;
|
Action<string, SugarParameter[]> onLogExecuted=null;
|
||||||
@@ -71,13 +69,15 @@ namespace Yi.Framework.SqlSugarCore
|
|||||||
|
|
||||||
onSqlSugarClientConfig += dependency.OnSqlSugarClientConfig;
|
onSqlSugarClientConfig += dependency.OnSqlSugarClientConfig;
|
||||||
}
|
}
|
||||||
|
//最先存放db操作
|
||||||
|
onSqlSugarClientConfig(sqlSugarClient);
|
||||||
|
|
||||||
sqlSugarClient.Aop.OnLogExecuting = onLogExecuting;
|
sqlSugarClient.Aop.OnLogExecuting = onLogExecuting;
|
||||||
sqlSugarClient.Aop.OnLogExecuted =onLogExecuted;
|
sqlSugarClient.Aop.OnLogExecuted =onLogExecuted;
|
||||||
|
|
||||||
sqlSugarClient.Aop.DataExecuting = dataExecuting;
|
sqlSugarClient.Aop.DataExecuting = dataExecuting;
|
||||||
sqlSugarClient.Aop.DataExecuted = dataExecuted;
|
sqlSugarClient.Aop.DataExecuted = dataExecuted;
|
||||||
onSqlSugarClientConfig(sqlSugarClient);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,26 +11,19 @@ namespace Yi.Framework.SqlSugarCore
|
|||||||
{
|
{
|
||||||
public static class SqlsugarCoreExtensions
|
public static class SqlsugarCoreExtensions
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddYiDbContext<DbContext>(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where DbContext : class, ISqlSugarDbContext
|
public static IServiceCollection AddYiDbContext<TDbContext>(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where TDbContext : class, ISqlSugarDbContextDependencies
|
||||||
{
|
{
|
||||||
service.Replace(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), serviceLifetime));
|
service.Add(new ServiceDescriptor(typeof(ISqlSugarDbContextDependencies), typeof(TDbContext), serviceLifetime));
|
||||||
return service;
|
|
||||||
}
|
|
||||||
public static IServiceCollection TryAddYiDbContext<DbContext>(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where DbContext : class, ISqlSugarDbContext
|
|
||||||
{
|
|
||||||
service.TryAdd(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), serviceLifetime));
|
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddYiDbContext<TDbContext>(this IServiceCollection service, Action<DbConnOptions> options) where TDbContext : class, ISqlSugarDbContextDependencies
|
||||||
public static IServiceCollection AddYiDbContext<DbContext>(this IServiceCollection service, Action<DbConnOptions> options) where DbContext : class, ISqlSugarDbContext
|
|
||||||
{
|
{
|
||||||
|
|
||||||
service.Configure<DbConnOptions>(ops =>
|
service.Configure<DbConnOptions>(ops =>
|
||||||
{
|
{
|
||||||
options.Invoke(ops);
|
options.Invoke(ops);
|
||||||
});
|
});
|
||||||
service.AddYiDbContext<DbContext>();
|
service.AddYiDbContext<TDbContext>();
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace Yi.Framework.Rbac.SqlSugarCore
|
|||||||
{
|
{
|
||||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||||
{
|
{
|
||||||
context.Services.TryAddYiDbContext<YiRbacDbContextFactory>();
|
context.Services.AddYiDbContext<YiRbacDbContext>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using SqlSugar;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Volo.Abp.DependencyInjection;
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Data;
|
||||||
|
using Volo.Abp.Users;
|
||||||
using Yi.Framework.Rbac.Domain.Authorization;
|
using Yi.Framework.Rbac.Domain.Authorization;
|
||||||
using Yi.Framework.Rbac.Domain.Entities;
|
using Yi.Framework.Rbac.Domain.Entities;
|
||||||
using Yi.Framework.Rbac.Domain.Extensions;
|
using Yi.Framework.Rbac.Domain.Extensions;
|
||||||
@@ -9,21 +11,16 @@ using Yi.Framework.SqlSugarCore;
|
|||||||
|
|
||||||
namespace Yi.Framework.Rbac.SqlSugarCore
|
namespace Yi.Framework.Rbac.SqlSugarCore
|
||||||
{
|
{
|
||||||
public class YiRbacDbContextFactory : SqlSugarDbContextFactory
|
public class YiRbacDbContext : SqlSugarDbContext
|
||||||
{
|
{
|
||||||
public YiRbacDbContextFactory(IAbpLazyServiceProvider lazyServiceProvider) : base(lazyServiceProvider)
|
protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService<IDataFilter>();
|
||||||
{
|
protected ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService<ICurrentUser>();
|
||||||
}
|
|
||||||
|
|
||||||
protected override void CustomDataFilter(ISqlSugarClient sqlSugarClient)
|
protected override void CustomDataFilter(ISqlSugarClient sqlSugarClient)
|
||||||
{
|
{
|
||||||
if (DataFilter.IsEnabled<IDataPermission>())
|
if (DataFilter.IsEnabled<IDataPermission>())
|
||||||
{
|
{
|
||||||
DataPermissionFilter(sqlSugarClient);
|
DataPermissionFilter(sqlSugarClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
base.CustomDataFilter(sqlSugarClient);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ namespace Yi.Abp.SqlsugarCore
|
|||||||
{
|
{
|
||||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||||
{
|
{
|
||||||
context.Services.AddYiDbContext<YiDbContextFactory>();
|
context.Services.AddYiDbContext<YiDbContext>();
|
||||||
//默认不开放,可根据项目需要是否Db直接对外开放
|
//默认不开放,可根据项目需要是否Db直接对外开放
|
||||||
//context.Services.AddTransient(x => x.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient);
|
//context.Services.AddTransient(x => x.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,7 @@ using Yi.Framework.SqlSugarCore;
|
|||||||
|
|
||||||
namespace Yi.Abp.SqlSugarCore
|
namespace Yi.Abp.SqlSugarCore
|
||||||
{
|
{
|
||||||
public class YiDbContextFactory : YiRbacDbContextFactory
|
public class YiDbContext : SqlSugarDbContext
|
||||||
{
|
{
|
||||||
public YiDbContextFactory(IAbpLazyServiceProvider lazyServiceProvider) : base(lazyServiceProvider)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user