Переглянути джерело

Merge branch 'next' of https://gitee.com/390620652/Admin.NET into next

苏智明 2 роки тому
батько
коміт
4db249ba91

+ 1 - 1
Admin.NET/Admin.NET.Core/Const/SqlSugarConst.cs

@@ -25,7 +25,7 @@ public class SqlSugarConst
     public const string PrimaryKey = "Id";
 
     /// <summary>
-    /// 仓储对象
+    /// 仓储实例
     /// </summary>
     public static ITenant ITenant { get; set; }
 

+ 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;
         }
 

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

@@ -6,6 +6,9 @@ namespace Admin.NET.Core;
 
 public static class SqlSugarSetup
 {
+    // 缓存所有仓储连接实例
+    private static ConcurrentDictionary<Type, ISqlSugarClient> iClientAttrDict = new();
+
     /// <summary>
     /// SqlSugar 上下文初始化
     /// </summary>
@@ -41,6 +44,7 @@ public static class SqlSugarSetup
         });
 
         services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
+        InitSqlSugarRepository(sqlSugar.AsTenant()); // 初始化仓储实例
         services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
         services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
 
@@ -369,4 +373,43 @@ public static class SqlSugarSetup
                 db.CodeFirst.SplitTables().InitTables(entityType);
         }
     }
+
+    /// <summary>
+    /// 初始化仓储连接实例
+    /// </summary>
+    /// <param name="iTenant"></param>
+    public static void InitSqlSugarRepository(ITenant iTenant)
+    {
+        // 主库仓储实例
+        var iClientMain = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
+        iClientAttrDict.TryAdd(typeof(SysTableAttribute), iClientMain);
+
+        // 日志库仓储实例
+        var iClientLog = iTenant.IsAnyConnection(SqlSugarConst.LogConfigId)
+                ? iTenant.GetConnectionScope(SqlSugarConst.LogConfigId)
+                : iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
+        iClientAttrDict.TryAdd(typeof(LogTableAttribute), iClientLog);
+
+        // 其他库仓储实例
+        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)
+        {
+            MethodInfo genericMethod = typeof(ITenant).GetMethod("GetConnectionScopeWithAttr");
+            MethodInfo constructedMethod = genericMethod.MakeGenericMethod(entityType);
+            ISqlSugarClient iClientAttr = constructedMethod.Invoke(iTenant, null) as ISqlSugarClient;
+            iClientAttrDict.TryAdd(entityType, iClientAttr);
+        }
+    }
+
+    /// <summary>
+    /// 获取指定仓储连接实例
+    /// </summary>
+    /// <param name="type"></param>
+    /// <returns></returns>
+    public static ISqlSugarClient GetConnectionScope(Type type)
+    {
+        iClientAttrDict.TryGetValue(type, out ISqlSugarClient iClient);
+        return iClient;
+    }
 }