소스 검색

简化全局查询过滤器实现,注释旧方法

新增全局查询过滤器 `AddTableFilter`,替代原 `SetDeletedEntityFilter` 方法的动态解析和缓存机制。注释掉旧方法及其调用,保留代码供参考。新实现更简洁直观,但需注意潜在功能差异。
xf 3 일 전
부모
커밋
96e9c2e084
1개의 변경된 파일45개의 추가작업 그리고 44개의 파일을 삭제
  1. 45 44
      src/Hotline.Repository.SqlSugar/Extensions/SqlSugarStartupExtensions.cs

+ 45 - 44
src/Hotline.Repository.SqlSugar/Extensions/SqlSugarStartupExtensions.cs

@@ -129,6 +129,7 @@ namespace Hotline.Repository.SqlSugar.Extensions
                 db =>
                 {
                     SetDbAop(db, services);
+                    db.QueryFilter.AddTableFilter<ISoftDelete>(entity => !entity.IsDeleted);
                 }
             );
 
@@ -331,52 +332,52 @@ namespace Hotline.Repository.SqlSugar.Extensions
                 }
             };
 
-            SetDeletedEntityFilter(db);
+            //SetDeletedEntityFilter(db);
         }
 
-        private static void SetDeletedEntityFilter(SqlSugarClient db)
-        {
-            var cacheKey = $"DbFilter:{db.CurrentConnectionConfig.ConfigId}:IsDeleted";
-            var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
-            if (tableFilterItemList == null)
-            {
-                // 获取基类实体数据表
-                var entityTypes = AppDomain.CurrentDomain.GetAssemblies()
-                    .SelectMany(d => d.GetTypes())
-                    .Where(d => !d.IsInterface
-                                && !d.IsAbstract
-                                && d.IsClass
-                                && d.GetInterfaces().Any(x => x == typeof(ISoftDelete)));
-                if (!entityTypes.Any()) return;
-
-                var tableFilterItems = new List<TableFilterItem<object>>();
-                foreach (var entityType in entityTypes)
-                {
-                    if (entityType.GetProperty("IsDeleted") is null) continue;
-                    //// 排除非当前数据库实体
-                    //var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
-                    //if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
-                    //    (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
-                    //    continue;
-
-                    var lambda = DynamicExpressionParser.ParseLambda(new[] {
-                        Expression.Parameter(entityType, "d") },
-                    typeof(bool),
-                    $"{nameof(SoftDeleteEntity.IsDeleted)} == @0", false);
-                    var tableFilterItem = new TableFilterItem<object>(entityType, lambda);
-                    tableFilterItems.Add(tableFilterItem);
-                    db.QueryFilter.Add(tableFilterItem);
-                }
-                db.DataCache.Add(cacheKey, tableFilterItems);
-            }
-            else
-            {
-                tableFilterItemList.ForEach(u =>
-                {
-                    db.QueryFilter.Add(u);
-                });
-            }
-        }
+        //private static void SetDeletedEntityFilter(SqlSugarClient db)
+        //{
+        //    var cacheKey = $"DbFilter:{db.CurrentConnectionConfig.ConfigId}:IsDeleted";
+        //    var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
+        //    if (tableFilterItemList == null)
+        //    {
+        //        // 获取基类实体数据表
+        //        var entityTypes = AppDomain.CurrentDomain.GetAssemblies()
+        //            .SelectMany(d => d.GetTypes())
+        //            .Where(d => !d.IsInterface
+        //                        && !d.IsAbstract
+        //                        && d.IsClass
+        //                        && d.GetInterfaces().Any(x => x == typeof(ISoftDelete)));
+        //        if (!entityTypes.Any()) return;
+
+        //        var tableFilterItems = new List<TableFilterItem<object>>();
+        //        foreach (var entityType in entityTypes)
+        //        {
+        //            if (entityType.GetProperty("IsDeleted") is null) continue;
+        //            //// 排除非当前数据库实体
+        //            //var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
+        //            //if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
+        //            //    (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
+        //            //    continue;
+
+        //            var lambda = DynamicExpressionParser.ParseLambda(new[] {
+        //                Expression.Parameter(entityType, "d") },
+        //            typeof(bool),
+        //            $"{nameof(SoftDeleteEntity.IsDeleted)} == @0", false);
+        //            var tableFilterItem = new TableFilterItem<object>(entityType, lambda);
+        //            tableFilterItems.Add(tableFilterItem);
+        //            db.QueryFilter.Add(tableFilterItem);
+        //        }
+        //        db.DataCache.Add(cacheKey, tableFilterItems);
+        //    }
+        //    else
+        //    {
+        //        tableFilterItemList.ForEach(u =>
+        //        {
+        //            db.QueryFilter.Add(u);
+        //        });
+        //    }
+        //}
 
         #endregion
     }