Loading enumerations from database
        Posted  
        
            by Mosh
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Mosh
        
        
        
        Published on 2010-04-05T10:39:54Z
        Indexed on 
            2010/04/05
            10:43 UTC
        
        
        Read the original article
        Hit count: 348
        
.NET
Hello,
I have a problem with mapping .NET enumerations to database tables. Imagine I have a table called Statuses with the following values:
StatusID | Name
1 Draft
2 Ready
...
...
In the application layer, I can either use a Repository to get all Statuses as an IList object. However, the problem with this approach is that I cannot reference a certain status in my business logic. For example, how can I implement something like this?
if (myObject.Status is Ready)
    do this
else if (myObject.Status is Draft)
    do that...
Since the statuses are loaded dynamically, I cannot tell for sure what particular Status object in the List represents the Draft or Ready status.
Alternatively, I could just use an enumeration like
public enum Statuses
{
    Draft, 
    Ready
};
Then I could easily use an enumeration in my business logic.
if (myObject.Status == Statuses.Draft)
    // do something...
However, the problem with this approach is that every time the user wants to modify the list of Statuses (adding a new status, or renaming an existing status) the application has to be re-compiled. We cannot load the statuses dynamically from the database.
Has anyone else come across a similar situation? Any solutions/patterns?
Cheers,
Mosh
© Stack Overflow or respective owner