diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs index 7600dfb1..c3644004 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs @@ -1,13 +1,10 @@ -using System; -using System.Collections; +using System.Collections; using System.Reflection; -using System.Security.Policy; using System.Text; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using SqlSugar; -using Volo.Abp; using Volo.Abp.Auditing; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; @@ -17,6 +14,7 @@ using Volo.Abp.Guids; using Volo.Abp.MultiTenancy; using Volo.Abp.Users; using Yi.Framework.SqlSugarCore.Abstractions; +using Check = Volo.Abp.Check; namespace Yi.Framework.SqlSugarCore { @@ -26,25 +24,23 @@ namespace Yi.Framework.SqlSugarCore /// SqlSugar 客户端 /// public ISqlSugarClient SqlSugarClient { get; private set; } - public ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService(); + + protected ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService(); private IAbpLazyServiceProvider LazyServiceProvider { get; } private IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetRequiredService(); - protected ILoggerFactory Logger => LazyServiceProvider.LazyGetRequiredService(); + private ILoggerFactory Logger => LazyServiceProvider.LazyGetRequiredService(); private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService(); - public IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService(); + protected IDataFilter DataFilter => LazyServiceProvider.LazyGetRequiredService(); protected virtual bool IsMultiTenantFilterEnabled => DataFilter?.IsEnabled() ?? false; protected virtual bool IsSoftDeleteFilterEnabled => DataFilter?.IsEnabled() ?? false; - public IEntityChangeEventHelper EntityChangeEventHelper => LazyServiceProvider.LazyGetService(NullEntityChangeEventHelper.Instance); + private IEntityChangeEventHelper EntityChangeEventHelper => LazyServiceProvider.LazyGetService(NullEntityChangeEventHelper.Instance); public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService>().Value; - public AbpDbConnectionOptions ConnectionOptions => LazyServiceProvider.LazyGetRequiredService>().Value; - - public ISerializeService SerializeService=> LazyServiceProvider.LazyGetRequiredService(); - - private ISqlSugarDbConnectionCreator _dbConnectionCreator; + private ISerializeService SerializeService=> LazyServiceProvider.LazyGetRequiredService(); + public void SetSqlSugarClient(ISqlSugarClient sqlSugarClient) { SqlSugarClient = sqlSugarClient; @@ -53,7 +49,6 @@ namespace Yi.Framework.SqlSugarCore { LazyServiceProvider = lazyServiceProvider; var connectionCreator = LazyServiceProvider.LazyGetRequiredService(); - _dbConnectionCreator = connectionCreator; connectionCreator.OnSqlSugarClientConfig = OnSqlSugarClientConfig; connectionCreator.EntityService = EntityService; connectionCreator.DataExecuting = DataExecuting; @@ -65,6 +60,7 @@ namespace Yi.Framework.SqlSugarCore options.ConnectionString = GetCurrentConnectionString(); options.DbType = GetCurrentDbType(); })); + //统一使用aop处理 connectionCreator.SetDbAop(SqlSugarClient); //替换默认序列化器 SqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SerializeService = SerializeService; @@ -76,23 +72,12 @@ namespace Yi.Framework.SqlSugarCore /// protected virtual string GetCurrentConnectionString() { - var defautlUrl = Options.Url ?? ConnectionOptions.GetConnectionStringOrNull(ConnectionStrings.DefaultConnectionStringName); - //如果未开启多租户,返回db url 或者 默认连接字符串 - if (!Options.EnabledSaasMultiTenancy) - { - return defautlUrl; - } - - //开启了多租户 var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService(); - var connectionString = connectionStringResolver.ResolveAsync().GetAwaiter().GetResult(); + var connectionString = connectionStringResolver.ResolveAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - - //没有检测到使用多租户功能,默认使用默认库即可 if (string.IsNullOrWhiteSpace(connectionString)) { - Volo.Abp.Check.NotNull(Options.Url, "租户默认库Defalut未找到"); - connectionString = defautlUrl; + Check.NotNull(Options.Url, "dbUrl未配置"); } return connectionString!; } @@ -107,7 +92,7 @@ namespace Yi.Framework.SqlSugarCore return dbTypeFromTenantName.Value; } } - Volo.Abp.Check.NotNull(Options.DbType, "默认DbType未配置!"); + Check.NotNull(Options.DbType, "默认DbType未配置!"); return Options.DbType!.Value; } @@ -210,23 +195,22 @@ namespace Yi.Framework.SqlSugarCore if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreationTime))) { //为空或者为默认最小值 - if (oldValue is null || DateTime.MinValue.Equals(oldValue)) + if (DateTime.MinValue.Equals(oldValue)) { entityInfo.SetValue(DateTime.Now); } } if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreatorId))) { - if (CurrentUser.Id != null) + if (CurrentUser.Id is not null) { entityInfo.SetValue(CurrentUser.Id); } } - - //插入时,需要租户id,先预留 + if (entityInfo.PropertyName.Equals(nameof(IMultiTenant.TenantId))) { - if (CurrentTenant is not null) + if (CurrentTenant.Id is not null) { entityInfo.SetValue(CurrentTenant.Id); } diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs index 09a660bc..2e0fe69b 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs @@ -48,7 +48,7 @@ namespace Yi.Framework.SqlSugarCore.Uow //获取当前连接字符串,未多租户时,默认为空 var connectionString = await ResolveConnectionStringAsync(connectionStringName); - var dbContextKey = $"{this.GetType().FullName}_{connectionString}"; + var dbContextKey = $"{this.GetType().Name}_{connectionString}"; var unitOfWork = UnitOfWorkManager.Current; diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs index 9f8aa952..281e6dbd 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs @@ -25,7 +25,8 @@ namespace Yi.Framework.SqlSugarCore { var service = context.Services; var configuration = service.GetConfiguration(); - Configure(configuration.GetSection("DbConnOptions")); + var section = configuration.GetSection("DbConnOptions"); + Configure(section); service.TryAddScoped(); @@ -40,7 +41,12 @@ namespace Yi.Framework.SqlSugarCore service.AddTransient(typeof(ISugarDbContextProvider<>), typeof(UnitOfWorkSqlsugarDbContextProvider<>)); //替换Sqlsugar默认序列化器,用来解决.Select()不支持嵌套对象/匿名对象的非公有访问器 值无法绑定,如Id属性 - context.Services.AddSingleton (); + context.Services.AddSingleton(); + + var dbConfig = section.Get(); + //将默认db传递给abp连接字符串模块 + Configure(x => { x.ConnectionStrings.Default = dbConfig.Url; }); + return Task.CompletedTask; } @@ -50,8 +56,8 @@ namespace Yi.Framework.SqlSugarCore //进行CodeFirst var service = context.ServiceProvider; var options = service.GetRequiredService>().Value; - - var _logger= service.GetRequiredService>(); + + var logger = service.GetRequiredService>(); StringBuilder sb = new StringBuilder(); @@ -65,13 +71,14 @@ namespace Yi.Framework.SqlSugarCore sb.AppendLine("==============================="); - _logger.LogInformation(sb.ToString()); + logger.LogInformation(sb.ToString()); //Todo:准备支持多租户种子数据及CodeFirst if (options.EnabledCodeFirst) { CodeFirst(service); } + if (options.EnabledDbSeed) { await DataSeedAsync(service); @@ -80,7 +87,6 @@ namespace Yi.Framework.SqlSugarCore private void CodeFirst(IServiceProvider service) { - var moduleContainer = service.GetRequiredService(); var db = service.GetRequiredService().SqlSugarClient; @@ -95,11 +101,11 @@ namespace Yi.Framework.SqlSugarCore .Where(x => x.GetCustomAttribute() != null) .Where(x => x.GetCustomAttribute() is null)); } + if (types.Count > 0) { db.CopyNew().CodeFirst.InitTables(types.ToArray()); } - } private async Task DataSeedAsync(IServiceProvider service) @@ -108,4 +114,4 @@ namespace Yi.Framework.SqlSugarCore await dataSeeder.SeedAsync(); } } -} +} \ No newline at end of file