feat: 完成ISqlSugarDbContextDependencies抽离

This commit is contained in:
chenchun
2024-11-19 11:19:13 +08:00
parent 4198b53996
commit 9acb157fae
8 changed files with 52 additions and 47 deletions

View File

@@ -10,14 +10,14 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
{ {
public interface ISqlSugarDbContext public interface ISqlSugarDbContext
{ {
// IAbpLazyServiceProvider LazyServiceProvider { get; set; } /// <summary>
/// SqlSugarDb
/// </summary>
ISqlSugarClient SqlSugarClient { get; } ISqlSugarClient SqlSugarClient { get; }
DbConnOptions Options { get; }
/// <summary> /// <summary>
/// 数据库备份 /// 数据库备份
/// </summary> /// </summary>
void BackupDataBase(); void BackupDataBase();
void SetSqlSugarClient(ISqlSugarClient sqlSugarClient);
} }
} }

View File

@@ -5,7 +5,7 @@ namespace Yi.Framework.SqlSugarCore;
public interface ISqlSugarDbContextDependencies public interface ISqlSugarDbContextDependencies
{ {
void OnSqlSugarClientConfig(ISqlSugarClient client); void OnSqlSugarClientConfig(ISqlSugarClient client);
void DataExecuted(object obj, DataAfterModel dataAfterModel); void DataExecuted(object obj, DataAfterModel dataAfterModel);
void DataExecuting(object obj, DataFilterModel dataAfterModel); void DataExecuting(object obj, DataFilterModel dataAfterModel);

View File

@@ -1,16 +1,9 @@
using System.Collections; using System.Reflection;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using SqlSugar; using SqlSugar;
using Volo.Abp.Auditing;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.DependencyInjection; 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.MultiTenancy;
using Volo.Abp.Users; using Volo.Abp.Users;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
@@ -18,7 +11,7 @@ using Check = Volo.Abp.Check;
namespace Yi.Framework.SqlSugarCore namespace Yi.Framework.SqlSugarCore
{ {
public class SqlSugarDbContext : ISqlSugarDbContext public class SqlSugarDbContextFactory : ISqlSugarDbContext
{ {
/// <summary> /// <summary>
/// SqlSugar 客户端 /// SqlSugar 客户端
@@ -28,42 +21,30 @@ namespace Yi.Framework.SqlSugarCore
protected ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService<ICurrentUser>(); protected ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService<ICurrentUser>();
private IAbpLazyServiceProvider LazyServiceProvider { get; } private IAbpLazyServiceProvider LazyServiceProvider { get; }
private IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetRequiredService<IGuidGenerator>();
private ILoggerFactory Logger => LazyServiceProvider.LazyGetRequiredService<ILoggerFactory>();
private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>(); private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService<IDataFilter>(); protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService<IDataFilter>();
protected virtual bool IsMultiTenantFilterEnabled => DataFilter?.IsEnabled<IMultiTenant>() ?? false;
protected virtual bool IsSoftDeleteFilterEnabled => DataFilter?.IsEnabled<ISoftDelete>() ?? false;
private IEntityChangeEventHelper EntityChangeEventHelper =>
LazyServiceProvider.LazyGetService<IEntityChangeEventHelper>(NullEntityChangeEventHelper.Instance);
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>();
private IEnumerable<ISqlSugarDbContextDependencies> SqlSugarDbContextDependencies=>LazyServiceProvider.LazyGetRequiredService<IEnumerable<ISqlSugarDbContextDependencies>>(); private IEnumerable<ISqlSugarDbContextDependencies> SqlSugarDbContextDependencies=>LazyServiceProvider.LazyGetRequiredService<IEnumerable<ISqlSugarDbContextDependencies>>();
public void SetSqlSugarClient(ISqlSugarClient sqlSugarClient)
{
SqlSugarClient = sqlSugarClient;
}
public SqlSugarDbContext(IAbpLazyServiceProvider lazyServiceProvider) public SqlSugarDbContextFactory(IAbpLazyServiceProvider lazyServiceProvider)
{ {
LazyServiceProvider = lazyServiceProvider; LazyServiceProvider = lazyServiceProvider;
var connectionCreators = LazyServiceProvider.LazyGetRequiredService<ISqlSugarDbContextDependencies>();
//获取连接配置
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);
//替换默认序列化器 //替换默认序列化器
SqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SerializeService = SerializeService; SqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SerializeService = SerializeService;
//最后一步将全程序dbcontext汇总
// Aop及多租户连接字符串和类型需要单独设置 // Aop及多租户连接字符串和类型需要单独设置
SetDbAop(SqlSugarClient); SetDbAop(SqlSugarClient);
} }
@@ -72,14 +53,32 @@ namespace Yi.Framework.SqlSugarCore
/// 构建Aop-sqlsugaraop在多租户模式中需单独设置 /// 构建Aop-sqlsugaraop在多租户模式中需单独设置
/// </summary> /// </summary>
/// <param name="sqlSugarClient"></param> /// <param name="sqlSugarClient"></param>
public void SetDbAop(ISqlSugarClient sqlSugarClient) protected virtual void SetDbAop(ISqlSugarClient sqlSugarClient)
{ {
//将所有ISqlSugarDbContextDependencies进行累加 //将所有ISqlSugarDbContextDependencies进行累加
sqlSugarClient.Aop.OnLogExecuting = this.OnLogExecuting; Action<string, SugarParameter[]> onLogExecuting=null;
sqlSugarClient.Aop.OnLogExecuted = this.OnLogExecuted; Action<string, SugarParameter[]> onLogExecuted=null;
sqlSugarClient.Aop.DataExecuting = this.DataExecuting; Action<object, DataFilterModel> dataExecuting = null;
sqlSugarClient.Aop.DataExecuted = this.DataExecuted; Action<object, DataAfterModel> dataExecuted = null;
OnSqlSugarClientConfig(currentDb); Action<ISqlSugarClient> onSqlSugarClientConfig = null;
foreach (var dependency in SqlSugarDbContextDependencies)
{
onLogExecuting+= dependency.OnLogExecuting;
onLogExecuted += dependency.OnLogExecuted;
dataExecuting += dependency.DataExecuting;
dataExecuted += dependency.DataExecuted;
onSqlSugarClientConfig += dependency.OnSqlSugarClientConfig;
}
sqlSugarClient.Aop.OnLogExecuting = onLogExecuting;
sqlSugarClient.Aop.OnLogExecuted =onLogExecuted;
sqlSugarClient.Aop.DataExecuting = dataExecuting;
sqlSugarClient.Aop.DataExecuted = dataExecuted;
onSqlSugarClientConfig(sqlSugarClient);
} }
/// <summary> /// <summary>
@@ -87,7 +86,7 @@ namespace Yi.Framework.SqlSugarCore
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
/// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentException"></exception>
public ConnectionConfig BuildConnectionConfig(Action<ConnectionConfig>? action=null) protected virtual ConnectionConfig BuildConnectionConfig(Action<ConnectionConfig>? action=null)
{ {
var dbConnOptions = Options; var dbConnOptions = Options;
#region options #region options
@@ -143,7 +142,13 @@ namespace Yi.Framework.SqlSugarCore
//将所有ISqlSugarDbContextDependencies的EntityService进行累加 //将所有ISqlSugarDbContextDependencies的EntityService进行累加
//额外的实体服务需要这里配置, //额外的实体服务需要这里配置,
EntityService(c, p);
Action<PropertyInfo, EntityColumnInfo> entityService = null;
foreach (var dependency in SqlSugarDbContextDependencies)
{
entityService += dependency.EntityService;
}
entityService(c, p);
} }
}, },
//这里多租户有个坑,这里配置是无效的 //这里多租户有个坑,这里配置是无效的
@@ -227,7 +232,7 @@ namespace Yi.Framework.SqlSugarCore
} }
public void BackupDataBase() public virtual void BackupDataBase()
{ {
string directoryName = "database_backup"; string directoryName = "database_backup";
string fileName = DateTime.Now.ToString($"yyyyMMdd_HHmmss") + $"_{SqlSugarClient.Ado.Connection.Database}"; string fileName = DateTime.Now.ToString($"yyyyMMdd_HHmmss") + $"_{SqlSugarClient.Ado.Connection.Database}";

View File

@@ -28,7 +28,7 @@ namespace Yi.Framework.SqlSugarCore
var section = configuration.GetSection("DbConnOptions"); var section = configuration.GetSection("DbConnOptions");
Configure<DbConnOptions>(section); Configure<DbConnOptions>(section);
service.TryAddScoped<ISqlSugarDbContext, SqlSugarDbContext>(); service.TryAddScoped<ISqlSugarDbContext, SqlSugarDbContextFactory>();
//不开放sqlsugarClient //不开放sqlsugarClient
//service.AddTransient<ISqlSugarClient>(x => x.GetRequiredService<ISqlsugarDbContext>().SqlSugarClient); //service.AddTransient<ISqlSugarClient>(x => x.GetRequiredService<ISqlsugarDbContext>().SqlSugarClient);

View File

@@ -15,7 +15,7 @@ namespace Yi.Framework.Rbac.SqlSugarCore
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
context.Services.TryAddYiDbContext<YiRbacDbContext>(); context.Services.TryAddYiDbContext<YiRbacDbContextFactory>();
} }
} }
} }

View File

@@ -9,9 +9,9 @@ using Yi.Framework.SqlSugarCore;
namespace Yi.Framework.Rbac.SqlSugarCore namespace Yi.Framework.Rbac.SqlSugarCore
{ {
public class YiRbacDbContext : SqlSugarDbContext public class YiRbacDbContextFactory : SqlSugarDbContextFactory
{ {
public YiRbacDbContext(IAbpLazyServiceProvider lazyServiceProvider) : base(lazyServiceProvider) public YiRbacDbContextFactory(IAbpLazyServiceProvider lazyServiceProvider) : base(lazyServiceProvider)
{ {
} }

View File

@@ -33,7 +33,7 @@ namespace Yi.Abp.SqlsugarCore
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
context.Services.AddYiDbContext<YiDbContext>(); context.Services.AddYiDbContext<YiDbContextFactory>();
//默认不开放可根据项目需要是否Db直接对外开放 //默认不开放可根据项目需要是否Db直接对外开放
//context.Services.AddTransient(x => x.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient); //context.Services.AddTransient(x => x.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient);
} }

View File

@@ -6,9 +6,9 @@ using Yi.Framework.SqlSugarCore;
namespace Yi.Abp.SqlSugarCore namespace Yi.Abp.SqlSugarCore
{ {
public class YiDbContext : YiRbacDbContext public class YiDbContextFactory : YiRbacDbContextFactory
{ {
public YiDbContext(IAbpLazyServiceProvider lazyServiceProvider) : base(lazyServiceProvider) public YiDbContextFactory(IAbpLazyServiceProvider lazyServiceProvider) : base(lazyServiceProvider)
{ {
} }
} }