Просмотр исходного кода

优化提高Repository模式下的性能。

13728789855 2 лет назад
Родитель
Сommit
35d66a5aef

+ 4 - 8
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -12,30 +12,26 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
 {
     public SqlSugarRepository()
     {
-        SqlSugarConst.ITenant ??= App.GetRequiredService<ISqlSugarClient>().AsTenant();
-        SqlSugarConst.MainDb ??= SqlSugarConst.ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
-        base.Context = SqlSugarConst.MainDb;
+        base.Context = SqlSugarSetup.GetConnectionScope(typeof(SysTableAttribute));
 
         // 若实体贴有多库特性,则返回指定库连接
         if (typeof(T).IsDefined(typeof(TenantAttribute), false))
         {
-            base.Context = SqlSugarConst.ITenant.GetConnectionScopeWithAttr<T>();
+            base.Context = SqlSugarSetup.GetConnectionScope(typeof(T));
             return;
         }
 
         // 若实体贴有日志表特性,则返回日志库连接
         if (typeof(T).IsDefined(typeof(LogTableAttribute), false))
         {
-            base.Context = SqlSugarConst.ITenant.IsAnyConnection(SqlSugarConst.LogConfigId)
-                ? SqlSugarConst.ITenant.GetConnectionScope(SqlSugarConst.LogConfigId)
-                : SqlSugarConst.ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
+            base.Context = SqlSugarSetup.GetConnectionScope(typeof(LogTableAttribute));
             return;
         }
 
         // 若实体贴有系统表特性,则返回默认库连接
         if (typeof(T).IsDefined(typeof(SysTableAttribute), false))
         {
-            base.Context = SqlSugarConst.ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
+            base.Context = SqlSugarSetup.GetConnectionScope(typeof(SysTableAttribute));
             return;
         }
 

+ 50 - 0
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -6,6 +6,9 @@ namespace Admin.NET.Core;
 
 public static class SqlSugarSetup
 {
+    // SqlSugar连接字典
+    private static ConcurrentDictionary<Type, ISqlSugarClient> iClientAttrDict = new ConcurrentDictionary<Type, ISqlSugarClient>();
+
     /// <summary>
     /// SqlSugar 上下文初始化
     /// </summary>
@@ -41,6 +44,7 @@ public static class SqlSugarSetup
         });
 
         services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
+        InitRepositorySqlSugarClient(sqlSugar.AsTenant());//初始化连接字典
         services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
         services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
 
@@ -369,4 +373,50 @@ public static class SqlSugarSetup
                 db.CodeFirst.SplitTables().InitTables(entityType);
         }
     }
+
+    /// <summary>
+    /// 初始化SqlSugar Repository连接字典
+    /// </summary>
+    /// <param name="iTenant"></param>
+    public static void InitRepositorySqlSugarClient(ITenant iTenant)
+    {
+
+        var iClientMain = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
+
+        var iClientLog = iTenant.IsAnyConnection(SqlSugarConst.LogConfigId)
+                ? iTenant.GetConnectionScope(SqlSugarConst.LogConfigId)
+                : iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
+
+        var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
+        && u.IsDefined(typeof(SugarTable), false) && u.GetCustomAttribute<TenantAttribute>() != null).ToList();
+
+
+        foreach (var entityType in entityTypes)
+        {
+            // 获取包含泛型方法<T>的类型的MethodInfo  
+            MethodInfo genericMethod = typeof(ITenant).GetMethod("GetConnectionScopeWithAttr");
+
+            // MakeGenericMethod创建一个特定于类型的泛型方法  
+            MethodInfo constructedMethod = genericMethod.MakeGenericMethod(entityType);
+
+            // 调用泛型方法  
+            ISqlSugarClient iClientAttr = constructedMethod.Invoke(iTenant, null) as ISqlSugarClient; // 对于静态方法,第一个参数是null  
+            iClientAttrDict.TryAdd(entityType, iClientAttr);
+        }
+
+        iClientAttrDict.TryAdd(typeof(SysTableAttribute), iClientMain);
+        iClientAttrDict.TryAdd(typeof(LogTableAttribute), iClientLog);
+    }
+
+    /// <summary>
+    /// 获取SqlSugar Repository连接字典
+    /// </summary>
+    /// <param name="type"></param>
+    /// <returns></returns>
+    public static ISqlSugarClient GetConnectionScope(Type type)
+    {
+        ISqlSugarClient iClient = null;
+        iClientAttrDict.TryGetValue(type, out iClient);
+        return iClient;
+    }
 }