feat: 移除转换器使用 PetaPoco 扩展程序集内置转化器

This commit is contained in:
Argo-Tianyi 2022-01-24 00:46:22 +08:00
parent f0a3c85f43
commit 8536fadf2c
3 changed files with 4 additions and 66 deletions

View File

@ -4,7 +4,7 @@
<PackageReference Include="BootstrapBlazor" Version="6.2.9-beta04" /> <PackageReference Include="BootstrapBlazor" Version="6.2.9-beta04" />
<PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" /> <PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.1" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.1" />
<PackageReference Include="PetaPoco.Extensions" Version="5.2.0" /> <PackageReference Include="PetaPoco.Extensions" Version="6.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,5 +1,4 @@
using BootstrapAdmin.DataAccess.PetaPoco.Coverters; using PetaPoco;
using PetaPoco;
using System.Reflection; using System.Reflection;
namespace BootstrapAdmin.DataAccess.PetaPoco; namespace BootstrapAdmin.DataAccess.PetaPoco;
@ -65,10 +64,10 @@ class BootstrapAdminConventionMapper : ConventionMapper
} }
public override Func<object?, object?> GetFromDbConverter(PropertyInfo targetProperty, Type sourceType) => targetProperty.PropertyType.IsEnum && sourceType == typeof(string) public override Func<object?, object?> 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); : base.GetFromDbConverter(targetProperty, sourceType);
public override Func<object?, object?> GetToDbConverter(PropertyInfo targetProperty) => targetProperty.PropertyType.IsEnum public override Func<object?, object?> GetToDbConverter(PropertyInfo targetProperty) => targetProperty.PropertyType.IsEnum
? new StringToEnumConverter(targetProperty.PropertyType).ConvertToDb ? new NumberToEnumConverter(targetProperty.PropertyType).ConvertToDb
: base.GetToDbConverter(targetProperty); : base.GetToDbConverter(targetProperty);
} }

View File

@ -1,61 +0,0 @@
namespace BootstrapAdmin.DataAccess.PetaPoco.Coverters;
/// <summary>
/// 字符串转枚举转换器
/// </summary>
class StringToEnumConverter
{
private Type TargetType { get; set; }
/// <summary>
///
/// </summary>
/// <param name="targetType"></param>
public StringToEnumConverter(Type targetType) => TargetType = targetType;
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
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;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
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;
}
}