Prechádzať zdrojové kódy

!1562 优化字典特性部分代码
Merge pull request !1562 from ir0nmax/next

zuohuaijun 1 rok pred
rodič
commit
2575413cd8

+ 19 - 18
Admin.NET/Admin.NET.Core/Attribute/DictAttribute.cs

@@ -13,6 +13,21 @@ namespace Admin.NET.Core;
 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
 public class DictAttribute : ValidationAttribute, ITransient
 public class DictAttribute : ValidationAttribute, ITransient
 {
 {
+    /// <summary>
+    /// 字典编码
+    /// </summary>
+    public string DictTypeCode { get; }
+
+    /// <summary>
+    /// 是否允许空字符串
+    /// </summary>
+    public bool AllowEmptyStrings { get; set; } = false;
+
+    /// <summary>
+    /// 允许空值,有值才验证,默认 false
+    /// </summary>
+    public bool AllowNullValue { get; set; } = false;
+
     /// <summary>
     /// <summary>
     /// 字典值合规性校验特性
     /// 字典值合规性校验特性
     /// </summary>
     /// </summary>
@@ -42,31 +57,17 @@ public class DictAttribute : ValidationAttribute, ITransient
 
 
         var sysDictDataServiceProvider = App.GetRequiredService<SysDictDataService>();
         var sysDictDataServiceProvider = App.GetRequiredService<SysDictDataService>();
         var dictDataList = sysDictDataServiceProvider.GetDataList(DictTypeCode).Result;
         var dictDataList = sysDictDataServiceProvider.GetDataList(DictTypeCode).Result;
-        
+
         // 获取枚举类型,可能存在Nullable类型,所以需要尝试获取最终类型
         // 获取枚举类型,可能存在Nullable类型,所以需要尝试获取最终类型
         var type = value?.GetType();
         var type = value?.GetType();
-        if (type != null) type = Nullable.GetUnderlyingType(type) ?? type;
+        type = type != null ? Nullable.GetUnderlyingType(type) ?? type : null;
+
         // 使用HashSet来提高查找效率
         // 使用HashSet来提高查找效率
         var valueList = (type?.IsEnum ?? DictTypeCode.EndsWith("Enum")) ? dictDataList.Select(u => u.Name) : dictDataList.Select(u => u.Value);
         var valueList = (type?.IsEnum ?? DictTypeCode.EndsWith("Enum")) ? dictDataList.Select(u => u.Name) : dictDataList.Select(u => u.Value);
         var dictHash = new HashSet<string>(valueList);
         var dictHash = new HashSet<string>(valueList);
-        
+
         if (!dictHash.Contains(valueAsString))
         if (!dictHash.Contains(valueAsString))
             return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!");
             return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!");
         return ValidationResult.Success;
         return ValidationResult.Success;
     }
     }
-
-    /// <summary>
-    /// 字典编码
-    /// </summary>
-    public string DictTypeCode { get; set; }
-
-    /// <summary>
-    /// 是否允许空字符串
-    /// </summary>
-    public bool AllowEmptyStrings { get; set; } = false;
-
-    /// <summary>
-    /// 允许空值,有值才验证,默认 false
-    /// </summary>
-    public bool AllowNullValue { get; set; } = false;
 }
 }