From 9acb157faee03b1968344a5746ad4b42e283ab58 Mon Sep 17 00:00:00 2001 From: chenchun Date: Tue, 19 Nov 2024 11:19:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90ISqlSugarDbContextDep?= =?UTF-8?q?endencies=E6=8A=BD=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ISqlSugarDbContext.cs | 6 +- .../ISqlSugarDbContextDependencies.cs | 2 +- ...Context.cs => SqlSugarDbContextFactory.cs} | 77 ++++++++++--------- .../YiFrameworkSqlSugarCoreModule.cs | 2 +- .../YiFrameworkRbacSqlSugarCoreModule.cs | 2 +- ...DbContext.cs => YiRbacDbContextFactory.cs} | 4 +- .../YiAbpSqlSugarCoreModule.cs | 2 +- .../{YiDbContext.cs => YiDbContextFactory.cs} | 4 +- 8 files changed, 52 insertions(+), 47 deletions(-) rename Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/{SqlSugarDbContext.cs => SqlSugarDbContextFactory.cs} (82%) rename Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/{YiRbacDbContext.cs => YiRbacDbContextFactory.cs} (95%) rename Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/{YiDbContext.cs => YiDbContextFactory.cs} (57%) diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/ISqlSugarDbContext.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/ISqlSugarDbContext.cs index 8fc6afa5..cd4f9dce 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/ISqlSugarDbContext.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore.Abstractions/ISqlSugarDbContext.cs @@ -10,14 +10,14 @@ namespace Yi.Framework.SqlSugarCore.Abstractions { public interface ISqlSugarDbContext { - // IAbpLazyServiceProvider LazyServiceProvider { get; set; } + /// + /// SqlSugarDb + /// ISqlSugarClient SqlSugarClient { get; } - DbConnOptions Options { get; } /// /// 数据库备份 /// void BackupDataBase(); - void SetSqlSugarClient(ISqlSugarClient sqlSugarClient); } } diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/ISqlSugarDbContextDependencies.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/ISqlSugarDbContextDependencies.cs index 37d75599..f3826abe 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/ISqlSugarDbContextDependencies.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/ISqlSugarDbContextDependencies.cs @@ -5,7 +5,7 @@ namespace Yi.Framework.SqlSugarCore; public interface ISqlSugarDbContextDependencies { - void OnSqlSugarClientConfig(ISqlSugarClient client); + void OnSqlSugarClientConfig(ISqlSugarClient client); void DataExecuted(object obj, DataAfterModel dataAfterModel); void DataExecuting(object obj, DataFilterModel dataAfterModel); diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextFactory.cs similarity index 82% rename from Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs rename to Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextFactory.cs index 247aad4a..8e5cc37e 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextFactory.cs @@ -1,16 +1,9 @@ -using System.Collections; -using System.Reflection; -using System.Text; +using System.Reflection; 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; @@ -18,7 +11,7 @@ using Check = Volo.Abp.Check; namespace Yi.Framework.SqlSugarCore { - public class SqlSugarDbContext : ISqlSugarDbContext + public class SqlSugarDbContextFactory : ISqlSugarDbContext { /// /// SqlSugar 客户端 @@ -28,42 +21,30 @@ namespace Yi.Framework.SqlSugarCore protected ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService(); private IAbpLazyServiceProvider LazyServiceProvider { get; } - private IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetRequiredService(); - private ILoggerFactory Logger => LazyServiceProvider.LazyGetRequiredService(); private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService(); protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService(); - protected virtual bool IsMultiTenantFilterEnabled => DataFilter?.IsEnabled() ?? false; - - protected virtual bool IsSoftDeleteFilterEnabled => DataFilter?.IsEnabled() ?? false; - - private IEntityChangeEventHelper EntityChangeEventHelper => - LazyServiceProvider.LazyGetService(NullEntityChangeEventHelper.Instance); public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService>().Value; private ISerializeService SerializeService => LazyServiceProvider.LazyGetRequiredService(); private IEnumerable SqlSugarDbContextDependencies=>LazyServiceProvider.LazyGetRequiredService>(); - public void SetSqlSugarClient(ISqlSugarClient sqlSugarClient) - { - SqlSugarClient = sqlSugarClient; - } - - public SqlSugarDbContext(IAbpLazyServiceProvider lazyServiceProvider) + + public SqlSugarDbContextFactory(IAbpLazyServiceProvider lazyServiceProvider) { LazyServiceProvider = lazyServiceProvider; - var connectionCreators = LazyServiceProvider.LazyGetRequiredService(); - + + //获取连接配置 var connectionConfig = BuildConnectionConfig(action: options => { options.ConnectionString = GetCurrentConnectionString(); options.DbType = GetCurrentDbType(); }); SqlSugarClient = new SqlSugarClient(connectionConfig); + //替换默认序列化器 SqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SerializeService = SerializeService; - - //最后一步,将全程序dbcontext汇总 + // Aop及多租户连接字符串和类型,需要单独设置 SetDbAop(SqlSugarClient); } @@ -72,14 +53,32 @@ namespace Yi.Framework.SqlSugarCore /// 构建Aop-sqlsugaraop在多租户模式中,需单独设置 /// /// - public void SetDbAop(ISqlSugarClient sqlSugarClient) + protected virtual void SetDbAop(ISqlSugarClient sqlSugarClient) { //将所有,ISqlSugarDbContextDependencies进行累加 - sqlSugarClient.Aop.OnLogExecuting = this.OnLogExecuting; - sqlSugarClient.Aop.OnLogExecuted = this.OnLogExecuted; - sqlSugarClient.Aop.DataExecuting = this.DataExecuting; - sqlSugarClient.Aop.DataExecuted = this.DataExecuted; - OnSqlSugarClientConfig(currentDb); + Action onLogExecuting=null; + Action onLogExecuted=null; + Action dataExecuting = null; + Action dataExecuted = null; + Action 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); + } /// @@ -87,7 +86,7 @@ namespace Yi.Framework.SqlSugarCore /// /// /// - public ConnectionConfig BuildConnectionConfig(Action? action=null) + protected virtual ConnectionConfig BuildConnectionConfig(Action? action=null) { var dbConnOptions = Options; #region 组装options @@ -143,7 +142,13 @@ namespace Yi.Framework.SqlSugarCore //将所有,ISqlSugarDbContextDependencies的EntityService进行累加 //额外的实体服务需要这里配置, - EntityService(c, p); + + Action 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 fileName = DateTime.Now.ToString($"yyyyMMdd_HHmmss") + $"_{SqlSugarClient.Ado.Connection.Database}"; diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs index 281e6dbd..d86d07f2 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs @@ -28,7 +28,7 @@ namespace Yi.Framework.SqlSugarCore var section = configuration.GetSection("DbConnOptions"); Configure(section); - service.TryAddScoped(); + service.TryAddScoped(); //不开放sqlsugarClient //service.AddTransient(x => x.GetRequiredService().SqlSugarClient); diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiFrameworkRbacSqlSugarCoreModule.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiFrameworkRbacSqlSugarCoreModule.cs index 32936300..135714b7 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiFrameworkRbacSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiFrameworkRbacSqlSugarCoreModule.cs @@ -15,7 +15,7 @@ namespace Yi.Framework.Rbac.SqlSugarCore { public override void ConfigureServices(ServiceConfigurationContext context) { - context.Services.TryAddYiDbContext(); + context.Services.TryAddYiDbContext(); } } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiRbacDbContext.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiRbacDbContextFactory.cs similarity index 95% rename from Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiRbacDbContext.cs rename to Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiRbacDbContextFactory.cs index 4d00b1a9..bbd94e06 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiRbacDbContext.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/YiRbacDbContextFactory.cs @@ -9,9 +9,9 @@ using Yi.Framework.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) { } diff --git a/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiAbpSqlSugarCoreModule.cs b/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiAbpSqlSugarCoreModule.cs index b3e27036..7f85fd00 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiAbpSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiAbpSqlSugarCoreModule.cs @@ -33,7 +33,7 @@ namespace Yi.Abp.SqlsugarCore { public override void ConfigureServices(ServiceConfigurationContext context) { - context.Services.AddYiDbContext(); + context.Services.AddYiDbContext(); //默认不开放,可根据项目需要是否Db直接对外开放 //context.Services.AddTransient(x => x.GetRequiredService().SqlSugarClient); } diff --git a/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiDbContext.cs b/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiDbContextFactory.cs similarity index 57% rename from Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiDbContext.cs rename to Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiDbContextFactory.cs index 28fd4436..9077cf18 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiDbContext.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.SqlSugarCore/YiDbContextFactory.cs @@ -6,9 +6,9 @@ using Yi.Framework.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) { } }