开发者

Best way to store enum values in database - String or Int

开发者 https://www.devze.com 2022-12-09 10:54 出处:网络
I have a number of enums in my application which are used as property type in some classes. What is the best way to store these values in database, as String or Int?

I have a number of enums in my application which are used as property type in some classes.

What is the best way to store these values in database, as String or Int?

FYI, I will also be mapping these attribute types using fluent Nhibernate.

Sample code:

public enum ReportOutputFormat
{
    DOCX,
    PDF,
    HTML
}

public enum ReportOutputMethod
{
    Save,
    Email,
    SaveAndEmail
}

public class ReportRequest
{
    public Int32 TemplateId
    {
        get { return templateId; }
        set { templateId = value; }
    }
    public ReportOutputFormat OutputFormat
    {
        get { return outputFormat; }
        set { outputFormat = value; }
    }

    public ReportOutputMethod Output开发者_JS百科Method
    {
        get { return outputMethod; }
        set { outputMethod = value; }
    }
}


The implementation is easy in both cases, and performance differences should be minor.

Therefore, go for the meaning : the Strings are more meaningful than numbers, so use the String.


Both have advantages. If you store them by their Integer value, then you must be careful editing your enumeration later on in your project. If somehow the integer values for the enumeration elements get redefined, all your data will be corrupted. But it will be the fastest/smallest datatype to use.

If you use the string representation you'll not have the redefined value problem for as long as you don't change the name of the elements in the enumeration. However strings take up more space in your database and are probably a bit more costly in use.

At the end, there's no definitive winner I guess.

Edit

Using the Integer value might be the best way. You can overcome the 'redefined value' problem for the enumeration elements, by setting the value for each element yourself. Indeed a good suggestion from Kevin.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号