From 8536fadf2cda4d1ec1437ea4b8ed9c5c929f24a8 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 24 Jan 2022 00:46:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=99=A8=E4=BD=BF=E7=94=A8=20PetaPoco=20=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E9=9B=86=E5=86=85=E7=BD=AE=E8=BD=AC=E5=8C=96?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapAdmin.DataAccess.PetaPoco.csproj | 2 +- .../BootstrapAdminConventionMapper.cs | 7 +-- .../Coverters/StringToEnumConverter.cs | 61 ------------------- 3 files changed, 4 insertions(+), 66 deletions(-) delete mode 100644 src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Coverters/StringToEnumConverter.cs diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj index d3b8ebe3..b73e89b7 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj @@ -4,7 +4,7 @@ - + diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs index 0bbaa2da..429c737f 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs @@ -1,5 +1,4 @@ -using BootstrapAdmin.DataAccess.PetaPoco.Coverters; -using PetaPoco; +using PetaPoco; using System.Reflection; namespace BootstrapAdmin.DataAccess.PetaPoco; @@ -65,10 +64,10 @@ class BootstrapAdminConventionMapper : ConventionMapper } public override Func GetFromDbConverter(PropertyInfo targetProperty, Type sourceType) => targetProperty.PropertyType.IsEnum && sourceType == typeof(string) - ? new StringToEnumConverter(targetProperty.PropertyType).ConvertFromDb + ? new NumberToEnumConverter(targetProperty.PropertyType).ConvertFromDb : base.GetFromDbConverter(targetProperty, sourceType); public override Func GetToDbConverter(PropertyInfo targetProperty) => targetProperty.PropertyType.IsEnum - ? new StringToEnumConverter(targetProperty.PropertyType).ConvertToDb + ? new NumberToEnumConverter(targetProperty.PropertyType).ConvertToDb : base.GetToDbConverter(targetProperty); } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Coverters/StringToEnumConverter.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Coverters/StringToEnumConverter.cs deleted file mode 100644 index 437ce0a5..00000000 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Coverters/StringToEnumConverter.cs +++ /dev/null @@ -1,61 +0,0 @@ -namespace BootstrapAdmin.DataAccess.PetaPoco.Coverters; - -/// -/// 字符串转枚举转换器 -/// -class StringToEnumConverter -{ - private Type TargetType { get; set; } - - /// - /// - /// - /// - public StringToEnumConverter(Type targetType) => TargetType = targetType; - - /// - /// - /// - /// - /// - /// - public object? ConvertFromDb(object? value) - { - if (value == null) - { - throw new ArgumentNullException(nameof(value)); - } - - object? ret; - if (value != null && Enum.TryParse(TargetType, value.ToString(), true, out var v)) - { - ret = v; - } - else - { - throw new InvalidCastException($"{value} 无法转化为 {TargetType.Name} 成员"); - } - return ret; - } - - /// - /// - /// - /// - /// - /// - public object? ConvertToDb(object? value) - { - object? ret; - var field = value?.ToString(); - if (!string.IsNullOrEmpty(field) && Enum.TryParse(TargetType, field, out var v)) - { - ret = v; - } - else - { - throw new InvalidCastException($"{TargetType.Name} 未定义 {field} 成员"); - } - return ret; - } -}