C# 存储库模式c中的枚举#

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

如何从数据库中读取在系统中是存储库模式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 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允许超出枚举范围的值。您必须进行一些额外的验证以保持数据的一致性

大家好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。您是使用EntityFramework还是仅使用普通ADO.net?ADO.net和DapperRepository欢迎使用堆栈溢出!抱歉,本网站要求您仅以英语发布问题。请你自己翻译;其他人为您翻译不会帮助您理解评论和答案,也不会回复反馈。非常感谢