我应该使用枚举作为鉴别器吗?

发布于 2024-08-13 03:38:16 字数 1685 浏览 1 评论 0 原文

枚举什么时候会崩溃?


为了支持现有系统中的新功能,我只是考虑对数据库模式中的实体表实施某种形式的鉴别器。

为了从做最少的事情开始,我首先决定在业务实体层使用整数列和 C# 枚举,以提高可读性。这将提供穷人的多态性,最终可能发展为实际的多态性,并可能发展为策略模式。

我决定咨询博客圈,因为我从来没有完全适应使用枚举 - 我想知道,我应该跳过枚举并直接使用结构还是类?:

首先,我发现了一个断言 '枚举是邪恶的',但我觉得这是过度概括并不能直接解决我的用例。

如果我确实选择枚举,那么会有一个关于如何通过添加额外的扩展我的里程的很好的讨论我的枚举的元数据。

接下来,我看到了 Jimmy Bogard 对 枚举 的讨论类并在“NHibernate 中的策略和判别器'

我应该跳过枚举并直接进入枚举类吗?或者是否有人对如何将简单的实体鉴别器添加到我的域模型有任何其他建议。

更新:

我还要补充一点,NHibernate 和 LINQ to SQL(可能还有所有其他与 ORM 相关的数据访问方法)都使枚举的使用非常有吸引力,因为它们 让您在映射中透明地映射鉴别器列

映射枚举类会那么容易吗?

相关问题:


免责声明:

尽管我粗心地使用了实体一词(带有小写的“e”),但我并不声称在这里讨论 DDD...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-08-20 03:38:16

今天枚举是邪恶的,明天 OOP 可能是邪恶的,而 AOP 会很好。

只需使用适合工作的正确工具即可。请记住,保持简单...

如果枚举只是为了告诉对象的类型 - 不要打扰,使用它。

如果其中包含一些业务逻辑,那么它可能是另一个类。

Today enums are evil, tomorrow OOP might be evil and AOP will be fine.

Just use the right tool for the job. Remember, Keep It Simple...

If the enum is just for the sake of telling the type of an object - don't bother, use it.

If it has some business logic in it, then it is probably another class.

凌乱心跳 2024-08-20 03:38:16

那些枚举类看起来很整洁,我经常嫉妒地看着 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文