应该使用枚举作为歧视者吗? [英] Should I ever use Enums as discriminators?

查看:136
本文介绍了应该使用枚举作为歧视者吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






为了支持现有系统中的新功能,我正在考虑实施在我的数据库模式中,对于一个实体表的某种形式的歧视。



要开始做最少的工作,我首先决定一个整数列,为了可读性,在业务实体层的C#枚举。这将提供可能最终成长为实际多态性的穷人的多态性,也许可能导致策略模式。



我决定咨询博客圈,因为我从来没有使用枚举完全舒服 - 我想知道,我应该跳过枚举并直接进行结构体或类吗?



首先我发现一个断言,一个href =http://devpinoy.org/blogs/cruizer/archive/2007/09/12/enums-are-evil.aspx =nofollow noreferrer>枚举是邪恶的',但我觉得这是一个过度泛化,并不直接解决我的用例。



如果我做一个枚举,那么有一个很好的讨论,我可以如何href =https://stackoverflow.com/questions/1008090/c-enum-values>通过在我的枚举中添加额外的元数据来扩展我的里程数。



接下来,我遇到了Jimmy Bogard对枚举类,并在 NHibernate中的策略和歧视者'



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



更新:



我还会补充说,NHibernate和LINQ to SQL(以及所有其他ORM相关的数据访问方法)都可以使用枚举非常诱人,因为它们让您在映射中透明地映射一个歧视列



映射枚举类是否容易?



相关问题:








免责声明:



尽管我粗心地使用术语实体(小写为'e')我不会在这里声称正在讨论DDD ...

解决方案

这些枚举类看起来很整洁,我经常嫉妒地看着Java的枚举。



我认为通常的规则适用:做最简单的事情可能会工作。如果您发现自己在枚举上有多个开关,那么这是一种气味,有时间考虑您发现的模式。



否则,为什么你不需要你自己的东西?


When do Enumerations break down?


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.

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.

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.

Update:

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?

Related questions:


Disclaimer:

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

解决方案

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?

这篇关于应该使用枚举作为歧视者吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆