我处于需要在数据库中存储付款类型枚举值以保存记录的情况。
问题在于我想为最终用户添加定义自己的值类型的能力。
我知道我可以在枚举中使用负范围作为我自己的值,因为用户定义的类型的id大于0但是这是一种正确的方法吗?
或许我应该有第二列,如CustomPaymentType,并引用PaymentType表以获得数据一致性?
答案 0 :(得分:1)
不要使用枚举。
枚举仅适用于按性质不变的事物,例如一周中的几天。
而是使用数据库中的引用表,如CustomPaymentType(Id,PaymentTypeName) 然后你可以使用一个看起来像这样的类:
public class CustomPaymentType
{
public string paymentTypeName { get; private set; }
public int Id { get; private set; }
// if you need "Constant payment types usable in code, just add something like:
public static CustomPaymentType CashPayment
{
get { return new CustomPaymentType() { Id = 7, paymentTypeName= "CashPayment" } }
}
public static CustomPaymentType CreditPayment
{
get { return new CustomPaymentType() { Id = 7,paymentTypeName= "CreditPayment" } }
}
}
这种方法非常好,因为您可以轻松编码编码时可能需要的众所周知的特定实例,而且它也非常容易扩展。