HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Storing Enum values as Strings in DB

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
valuesenumstoringstrings

Problem

I have a Data First Migration and have been struggling a little with storing an enum value as a string.

(Im aware that enums should be stored as ints but, personally I have never liked this, yes enums are hard and fast values but I have always percived them to vary a little bit as the code evolves, i am therefore worried about the integrity of using an int value as new values are added.)

However in my example I need to store the value as a string anyway so that other systems will be able to understand it.

So the database has the following generated property

public string billing_location { get; set; }


The View Model

public string billing_location { get; set; }

public MobiusBillingLocation BillingLocation
{
    get
    {
        MobiusBillingLocation mbl;
        if (Enum.TryParse(billing_location, true, out mbl))
        {
            return (MobiusBillingLocation)      Enum.Parse(typeof(MobiusBillingLocation), billing_location, true);
        }

        return MobiusBillingLocation.None;
    }
    //This also needs setting in the controller code
    set { billing_location = value == MobiusBillingLocation.None ? null : value.ToString(); }
}

public enum MobiusBillingLocation { None, UK, US, SGSingapore }


The code has been designed so that it will be happy with null values and translates None as null and back again.

My questions are, does this approach seem reasonable?
Any ideas / suggestions for improvments?
Would you use this approach for enums on a code first model for example?

UPDATE

I will add that, in this database, due to it being legacy and unnormalized (thats another topic) and has systems on it that i can't touch I cant be adding extra lookup tables at this time, the data in it however needs to have meaning to a user, ie integer values won't work as they don't pass meaning.

I am however also interested in some side discussion about use of enums.

Solution

There are clear guidelines for Enum Design where one of them says:


X DO NOT use an enum for open sets (such as the operating system version, names of your friends, etc.).

and this

public enum MobiusBillingLocation { None, UK, US, SGSingapore }


looks like an open set. const values might be a better solution in this case. You won't have to convert anything and you can use any names for the values.

public static class MobiusBillingLocation
{ 
    public const string None = null;
    public const string GreatBritan = "UK";
    public const string UnitedStates = "US";
    public const string SomethingSingapure = "SGSingapore";
}

Code Snippets

public enum MobiusBillingLocation { None, UK, US, SGSingapore }
public static class MobiusBillingLocation
{ 
    public const string None = null;
    public const string GreatBritan = "UK";
    public const string UnitedStates = "US";
    public const string SomethingSingapure = "SGSingapore";
}

Context

StackExchange Code Review Q#154676, answer score: 18

Revisions (0)

No revisions yet.