Dapper 查询时智能枚举类型转换
设置类型转换处理类:
using APS.Core.ProductionMaterial.ProductionResourceAggregate;
using Dapper;
namespace APS.Infrastructure.Data.DapperRepositories;
public class ResourceTypeHandler : SqlMapper.TypeHandler<ResourceType>
{
public override ResourceType Parse(object value)
{
return ResourceType.FromValue(Convert.ToInt32(value));
}
public override void SetValue(IDbDataParameter parameter, ResourceType value)
{
parameter.Value = value.Value;
}
}
在 program 中添加服务类:
SqlMapper.AddTypeHandler(new ResourceTypeHandler());
/// <summary>
/// 资源类型
/// </summary>
public class ResourceType : SmartEnum<ResourceType>
{
/// <summary>
/// 无
/// </summary>
public static readonly ResourceType None = new(nameof(None), 0);
/// <summary>
/// 人工
/// </summary>
public static readonly ResourceType Worker = new(nameof(Worker), 1);
/// <summary>
/// 机器
/// </summary>
public static readonly ResourceType Machine = new(nameof(Machine), 2);
public ResourceType(string name, int value) : base(name, value) { }
}
浙公网安备 33010602011771号