Enumeration class in the Repository Pattern

Asked

Viewed 120 times

3

How can I read from my bank an attribute int which in my system is a type attribute Enumeration in the Repository Pattern?

I took a class:

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)
    {
    }
}

Right in class Bank I put a type attribute Status;

Time to read from the bank where my class Bank and a table with an attribute Status guy int, but the attribute remains null.

How can I fix this?

  • Just reinforcing what this reading code is?

  • Como posso ler do meu banco um atributo int que no meu sistema, in your question has this, by chance in addition to this code has the code that retrieves the information contained in the bank ?

  • I don’t understand what you mean by reading code. I use ADO.net with Dapperpository

  • Yes I have a Service class that calls the Getall method of Repository

  • @Daniel put this specific code of your question?

  • public Ienumerable<Bankviewmodel> Getall() { Return _mapper. Map<Ienumerable<Bankviewmodel>> (_bankRepository.Findall()); }

  • except that when I do the reading except the Mapper if I do _bankRepository.Findall() it no longer brings the Status, then I discarded the Mapper as a problem, I think the problem is that the Enum is of type class that inherits from Enumerable

  • Pass this package please Dapperpository, the right name?

  • Microorm.Dapper.Repositories downloaded from Noget

  • was reading the comments is Enumerable or Enumeration???, because I thought it was something from Dapper, but I didn’t find anything. Although it doesn’t make much sense to use it like this

  • How is the method for searching in the bank? Taking advantage, the bank contains the integer values?

  • @Randrade is the type int yes in the bank, the problem that should be used a Enum, but, he wants to translate this into a class he inherits from a Enumeration, uses Dapper Repository layer. It seems that this is not very valid and does not work seems.

  • @Virgilionovic Accept I’m pretty sure it doesn’t accept. My doubt was just to know how you really are doing. The Github’s own package shows an example of how to do this. However, I would like to understand if he was doing something beyond what is in the question.

Show 8 more comments

2 answers

3


Thanks for the messages, I’ve solved the problem.

Create in the class that inherits from Enumeration the following:

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();
    }
  }**
}

So in the Bank class I did the following:

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;
     }
   }
}

Adding a Statusid attribute which is the type of bank attribute.

1

If you are using the Entity Framework it automatically identifies, that is, saved as int in the database and creates the object with the enumeration. What I want you to do is exchange this enumeration for something like this:

public enum Status
{
    Active,
    Inactive,
    Removed
}

Know that enums are not saved in the database, it will be an int column of the table that has Enum as property.

  • I know in this case it works only that I’m applying the example I posted above that in theory it should work too

  • I believe that treating Enum as inheritance for a class makes the Entity (I’m guessing you use EF) compute that it is a relationship, so the property is coming null. But I’m not sure anymore.

  • I am not using EF I am using Utilizo ADO.net with Dapperpository

Browser other questions tagged

You are not signed in. Login or sign up in order to post.