Enums or Tables?

2020-05-27 11:12发布

I am making this a community wiki, as I would appreciate people's approach and not necessarily an answer.

I am in the situation where I have a lot of lookup type data fields, that do not change. An example would be:

Yearly Salary
Option: 0 - 25K
Option: 25K - 100K
Option: 100K +

I would like to have these options easily available through an enum, but would also like to textual values available in the DB, as I will do reporting on the textual values and not a ID. Also, since they are static I do not want to be making calls to the DB.

I was thinking duplicating this in an enum and table, but would like to hear some alternate thoughts.

Thanks

标签: c# enums
9条回答
来,给爷笑一个
2楼-- · 2020-05-27 11:49

One way is to write a formatter that can turn you enum into string representations:

public class SalaryFormatter : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
         return (formatType == typeof(ICustomFormatter)) ? new
         SalaryFormatter () : null;
    }

    public string Format(string format, object o, IFormatProvider formatProvider)
    {
        if (o.GetType().Equals(typeof(Salary)))
        {
            return o.ToString();

            Salary salary = (Salary)o;
            switch (salary)
            {
                case Salary.Low:
                     return "0 - 25K";
                case Salary.Mid:
                     return "25K - 100K";
                case Salary.High:
                     return "100K+";
                default:
                     return salary.ToString();
            }
        }

    return o.ToString();
    }
}

You use the formatter like any other formatter:

Console.WriteLine(String.Format(new SalaryFormatter(), "Salary: {0}", salary));

The formatter can be extented to support different formats through formatting strings, multiple types, localization and so on.

查看更多
虎瘦雄心在
3楼-- · 2020-05-27 11:51

Use both, And you should investigate the CodeDOM. using this you can write code generation routines that allow the compilation process to automatically generate an assembly or class with these enums in it, by reading the database. This way you get to let the database drive, but you are not making calls to the database everytime you access an instance of the enum...

查看更多
beautiful°
4楼-- · 2020-05-27 11:52

For static items I use Enum with [Description()] attribute for each element. And T4 template to regenerate enum with descriptions on build (or whenever you want)

public enum EnumSalary
    {
        [Description("0 - 25K")] Low,
        [Description("25K - 100K")] Mid,
        [Description("100K+")] High
    }

And use it like

string str = EnumSalary.Mid.Description()

P.S. also created extension for System.Enum

public static string Description(this Enum value) {
    FieldInfo fi = value.GetType().GetField(value.ToString());
    var attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof(DescriptionAttribute), false );
    return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}

and reverse to create enum by description

public static TEnum ToDescriptionEnum<TEnum>(this string description)
{
    Type enumType = typeof(TEnum);
    foreach (string name in Enum.GetNames(enumType))
    {
        var enValue = Enum.Parse(enumType, name);
        if (Description((Enum)enValue).Equals(description)) {
            return (TEnum) enValue;
        }
    }
    throw new TargetException("The string is not a description or value of the specified enum.");
}
查看更多
登录 后发表回答