C# 存储库模式c中的枚举#
如何从数据库中读取在系统中是存储库模式C#中的枚举类型属性的int属性 我上了一节课:C# 存储库模式c中的枚举#,c#,repository-pattern,C#,Repository Pattern,如何从数据库中读取在系统中是存储库模式C#中的枚举类型属性的int属性 我上了一节课: public class Status : Enumeration { public static readonly Status Active = new Status(0, "Active"); public static readonly Status Inactive = new Status(1, "Inactive"); public static readonly Sta
public class Status : Enumeration
{
public static readonly Status Active = new Status(0, "Active");
public static readonly Status Inactive = new Status(1, "Inactive");
public static readonly Status Removed = new Status(2, "Removed");
public Status()
{
}
private Status(int value, string displayName)
: base(value, displayName)
{
}
}
所以在Bank类中,我放置了一个Status属性
在读取我的银行类所在的银行以及属性状态类型为int的表时,加上属性为null。例如,我将介绍一种静态方法,将类中的整数转换为“enum”
public class Status : Enumeration
{
//YOUR CODE
public static Status FromInteger(int value){
switch(value){
case 0:
return Active;
case 1:
return Inactive;
case 2:
return Removed;
default:
throw new ArgumentException();
}
}
}
我没有处理过DapperRepository。但快速查看ClassMapper
可以发现,您可以使用AutoMap方法利用自定义转换。但是,我既没有找到文档,也没有找到示例。
因此,我只能提出一个普遍的解决办法
public class Bank {
//Standard fields that are mapped to a table in database
public Status StatusEnum {
get {
Status.FromInteger(StatusId); //StatusId is a property mapped to table's field
}
set {
//TODO Handle null value
StatusId = value.Value;
}
}
}
注:
- 我假设属性
是映射到包含状态值的表字段的字段StatusId
- 此代码有明显的问题-StatusId允许超出枚举范围的值。您必须进行一些额外的验证以保持数据的一致性