Enum type int values

Posts   
 
    
MJC
User
Posts: 50
Joined: 06-May-2004
# Posted on: 15-May-2013 00:18:32   

Just curious if anybody does this or not, and how they may handle this differently.

This is probably my third project where I've leveraged this type of implementation where I: - Create a plugin in designer to create a property for each entity type / view / typed list / stored proc that holds a unique integer for that entity (the plugin just assigns the next integer across all elements of the same type if a value hasn't already been added) - Create a custom template that gets executed to override the constant enums templates so that enums (EntityType, TypedViewType, and I also add a TypedListType) give each item a unique value


public enum EntityType: int
{
    Customer = 4
    Group = 2,
    Order = 3,
    Person = 1,
}

  • This is particularly useful when using CTE queries and/or easily storing information in generic tables (like config settings managed by admins and/or users per entity), where you can have one column be the EntityTypeId, and an another EntityId, and you can have a true PK to pass around too across the entire database and stored as perma-key in external systems too (i.e.: 1/2 => typeid=1/pkid=2, in the case above Person entity with id 2)

I've thought to myself that it would be nice to have something in the designer where I could just easily check a box and it cache the int values of the enums (if desired, default would be not to as it does today for good reason) out of the box. It works fine doing it with a plugin and a template override, but I figured I'm probably not the only one that's needed this, or found a way to do this. Or are others just doing this type of thing by using the Name of the entity? Note, I don't abuse this, as I realize leveraging this EAV model type of anti-pattern isn't always the best thing to do, but it does come in extremely handy once in a while (also for storing stuff in search indexes for example).

Just curious, thoughts? Good idea? Bad idea? Good design? Bad design?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-May-2013 07:50:04   

Hi MJC,

I see your point. Although I don't see any customer in the forum asking for this before. Your solution is ok. However, changing the ConstansEnums.cs with your override template might not be the best approach, IMHO. Maybe you could create a separate template that creates a dictionary for this, like:

// sure you can find a better name for this :)
public class EntityTypeToIntConverter
{
     private Dictionary<EntityType, int> _entityTypeToInt;

     public EntityTypeToIntConverter()
     {
          // init the dictionary...
     }

     public EntityTypeToInt(EntityType type)
     {
          return _entityTypeToInt[type];
     }

     //...
}

You could pack your template + plugin + .tasks + .preset so others can easily use it in their projects simple_smile

David Elizondo | LLBLGen Support Team
MJC
User
Posts: 50
Joined: 06-May-2004
# Posted on: 15-May-2013 08:27:24   

Good idea, I like it.