From 14db420cbeb0d7f21962d4f8d6f5903294cef1ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Tue, 11 Jun 2024 22:34:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0db=E4=BD=9C=E7=94=A8?= =?UTF-8?q?=E5=9F=9F=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SqlsugarCoreExtensions.cs | 15 ++++++++------- .../Uow/UnitOfWorkSqlsugarDbContextProvider.cs | 13 +++++++------ .../Yi.Abp.Application/Services/TestService.cs | 5 +++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs index 7269e9d0..cdafb998 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlsugarCoreExtensions.cs @@ -11,22 +11,23 @@ namespace Yi.Framework.SqlSugarCore { public static class SqlsugarCoreExtensions { - public static IServiceCollection AddYiDbContext(this IServiceCollection service) where DbContext : class, ISqlSugarDbContext + public static IServiceCollection AddYiDbContext(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where DbContext : class, ISqlSugarDbContext { - service.Replace(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), ServiceLifetime.Transient)); + service.Replace(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), serviceLifetime)); return service; } - public static IServiceCollection TryAddYiDbContext(this IServiceCollection service) where DbContext : class, ISqlSugarDbContext + public static IServiceCollection TryAddYiDbContext(this IServiceCollection service, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where DbContext : class, ISqlSugarDbContext { - service.TryAdd(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), ServiceLifetime.Transient)); + service.TryAdd(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(DbContext), serviceLifetime)); return service; } - public static IServiceCollection AddYiDbContext(this IServiceCollection service,Action options) where DbContext : class, ISqlSugarDbContext + public static IServiceCollection AddYiDbContext(this IServiceCollection service, Action options) where DbContext : class, ISqlSugarDbContext { - - service.Configure(ops => { + + service.Configure(ops => + { options.Invoke(ops); }); service.AddYiDbContext(); 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 8fe45bae..b76e6c8d 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs @@ -52,13 +52,13 @@ namespace Yi.Framework.SqlSugarCore.Uow var unitOfWork = UnitOfWorkManager.Current; - if (unitOfWork == null || unitOfWork.Options.IsTransactional == false) + if (unitOfWork == null /*|| unitOfWork.Options.IsTransactional == false*/) { - if (ContextInstance.Current is null) - { - ContextInstance.Current = (TDbContext)ServiceProvider.GetRequiredService(); - } - var dbContext = (TDbContext)ContextInstance.Current; + //if (ContextInstance.Current is null) + //{ + // ContextInstance.Current = ; + //} + var dbContext = (TDbContext)ServiceProvider.GetRequiredService(); //提高体验,取消工作单元强制性 //throw new AbpException("A DbContext can only be created inside a unit of work!"); //如果不启用工作单元,创建一个新的db,不开启事务即可 @@ -81,6 +81,7 @@ namespace Yi.Framework.SqlSugarCore.Uow CreateDbContextAsync(unitOfWork, connectionStringName, connectionString).Result ); + await Console.Out.WriteLineAsync(">>>----------------实例化了db"+ ((SqlSugarDatabaseApi)databaseApi).DbContext.SqlSugarClient.ContextID.ToString()); //创建的db加入到当前工作单元中 unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); diff --git a/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs b/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs index d3180d46..e8c6bcb2 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs @@ -55,9 +55,10 @@ namespace Yi.Abp.Application.Services //魔改 // 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。 // 自动在各个情况处理db客户端最优解之一 - int i = 10; + int i = 3; List tasks = new List(); - + await sqlSugarRepository.GetListAsync(); + await sqlSugarRepository.GetListAsync(); while (i > 0) { tasks.Add(Task.Run(async () =>