perf: 改用sqlsugar db上下文对象

This commit is contained in:
陈淳
2023-03-14 09:22:20 +08:00
parent 9600e450af
commit cd9e27bcf3
3 changed files with 10 additions and 6 deletions

View File

@@ -24,12 +24,14 @@ namespace Yi.Framework.Core.Sqlsugar.Extensions
/// </summary>
public static class SqlsugarExtensions
{
//使用上下文对象
public static void AddDbSqlsugarContextServer(this IServiceCollection services)
{
services.AddTransient(x => x.GetRequiredService<SqlSugarDbContext>().SqlSugarClient);
services.AddTransient<SqlSugarDbContext>();
services.AddSingleton<ISqlSugarClient>(x => x.GetRequiredService<SqlSugarDbContext>().SqlSugarClient);
services.AddSingleton<SqlSugarDbContext>();
}
//直接使用sqlsugar
public static void AddSqlsugarServer(this IServiceCollection services, Action<SqlSugarClient>? action = null)
{
var dbConnOptions = Appsettings.app<DbConnOptions>("DbConnOptions");

View File

@@ -21,7 +21,7 @@ namespace Yi.Framework.Core.Sqlsugar
/// <summary>
/// SqlSugar 客户端
/// </summary>
public ISqlSugarClient SqlSugarClient { get; set; }
public ISqlSugarClient SqlSugarClient { get; set; }
protected ICurrentUser _currentUser;
@@ -33,7 +33,7 @@ namespace Yi.Framework.Core.Sqlsugar
{
_currentUser = currentUser;
_logger = logger;
_options= options;
_options = options;
var dbConnOptions = options.Value;
#region options
if (dbConnOptions.DbType is null)
@@ -57,7 +57,7 @@ namespace Yi.Framework.Core.Sqlsugar
});
}
#endregion
ISqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
SqlSugarClient = new SqlSugarScope(new ConnectionConfig()
{
//准备添加分表分库
DbType = dbConnOptions.DbType ?? DbType.Sqlite,

View File

@@ -28,7 +28,9 @@ namespace Yi.Framework.Core.Sqlsugar
services.Replace(new ServiceDescriptor(typeof(IUnitOfWorkManager), typeof(SqlsugarUnitOfWorkManager), ServiceLifetime.Singleton));
services.Replace(new ServiceDescriptor(typeof(IDataFilter), typeof(SqlsugarDataFilter), ServiceLifetime.Scoped));
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
services.AddSqlsugarServer();
//使用db上下文
services.AddDbSqlsugarContextServer();
}
}