我应该永远使用枚举作为鉴别?

2023-09-03 03:57:56 作者:强国联兵

在不枚举打破?

要支持新的功能,在现有的系统,我只是在考虑我的数据库架构实施某种形式的鉴别一个实体的表。

To support a new feature in an existing system, I was just considering implementing some form of discriminator to an entity table in my database schema.

在想要开始这样做,将工作我首先决定了一个整数列和C#枚举的业务实体层为了便于阅读的最少的事情。这将为贫困人的基因多态性可能最终成长为一个实际的多态性,或许对一个策略模式。

In wanting to begin by doing the least thing that will work I first of all decided on an integer column and a C# enum at the business entity layer for the sake of readability. This would provide the poor-man's polymorphism that may eventually grow into an actual polymorphism and perhaps towards a Strategy pattern.

我决定咨询博客,因为我从来没有完全舒适的使用枚举 - 我不知道,我应该跳过枚举,直走一个结构或类:

I decided to consult the blogosphere as I've never been entirely comfortable with using enums - I wondered, should I skip the enum and go straight for a struct or a class?:

首先,我找到了一个说法,即'的枚举是邪恶',但我觉得这是一种过度概括它并不直接针对我的用例。

First of all I found an assertion that 'Enums are Evil', but I felt this was an over-generalization which does not directly address my use-case.

如果我去一个枚举再有就是如何我可以延长我的里程商量好了通过添加额外的元数据到我的枚举。

If I do go for an enum then there's a good discussion of how I can extend my mileage by adding extra metadata to my enum.

接下来,我碰到了的枚举类,并在策略和鉴别的NHibernate的

Next, I came across Jimmy Bogard's discussions of Enumeration Classes and further discussed in 'Strategies and discriminators in NHibernate'

我应该跳过枚举和直行枚举类?或者有没有人有任何其他建议如何将一个简单的实体鉴别添加到我的域模型。

Should I skip the enums and go straight for Enumeration Classes? Or does anyone have any other suggestions for how to add a simple entity discriminator to my domain model.

更新:

我还补充一点,无论NHibernate和LINQ到SQL(也许所有其它的ORM相关的数据访问方法)使用枚举非常诱人的,因为他们的让你在测绘地图鉴别列透明。

I'd also add that both NHibernate and LINQ to SQL (and probably all other ORM-related data-access methods) make the use of enums very enticing as they let you map a discriminator column transparently in the mapping.

难道是那么容易映射一个枚举类?

Would it be as easy to map an Enumeration Class?

相关问题:

/492096/persisting-data-suited-for-enums /746812/best-practices-for-using-and-persisting-enums /492096/persisting-data-suited-for-enums /746812/best-practices-for-using-and-persisting-enums

免责声明:

尽管我不小心使用术语实体(以小写的'e')我不主张在这里讨论DDD ...

Despite my careless use of the term entity (with a lower case 'e') I don't claim to be discussing DDD here...

推荐答案

这些枚举类看起来整洁,我常常看着小心翼翼地在Java的枚举。

Those Enumeration Classes look neat, I've often looked jealously at Java's enums.

我觉得一般的规则:做最简单的事情可以运转。如果你发现自己有多个在枚举开关,那么这是一种气味,和时间来考虑你已经找到了模式。

I think the usual rules apply: do the Simplest Thing That Could Possibly Work. If you find yourself with more than one switch on the enum, then that's a smell, and time to consider the pattern you've found.

另外,为什么有一些负担,自己就不需要?

Otherwise, why burden yourself with something you don't need?

相关推荐