枚举的单独类? [英] Separate class for enums?

查看:70
本文介绍了枚举的单独类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于业务层中的一个单独的类来存储枚举的定义的普遍共识是什么?这是不好的做法吗?这是否符合良好的 n 层设计?目前我的枚举定义散布在不同的,我认为是相关的类周围 - 但我觉得它们应该在一个地方.事实上,这是否是一个主观问题,并且与我如何构建解决方案的其余部分有关?

What is the general consensus towards a separate class in the business layer to store the definition of enums? Is this bad practice? Does this conform to good n-tier design? At the moment my enum definitions are dotted around different, what I would deem as, relevant classes - but I feel as though they should be in one place. Is this, in fact, a subjective question and relative to how I've structured the rest of the solution?

推荐答案

将枚​​举保存在单独的类中

在这种情况下,您将不相关的定义扔到一个类中,几乎没有任何好处.

In this case you're tossing unrelated definitions into one class, for almost no benefits.

将枚​​举定义为与其相关的类的嵌套类型

当你在一个类中持有枚举时,你可能会遇到命名问题:

When you hold enums within a class, you may run into naming troubles:

class Foo
{
    public enum SomeType { /* ... */ }
    public SomeType SomeType { get; set; }
}

这会给出 SomeType 已经定义的错误.

This would give an error that SomeType is already defined.

这可能只是个人喜好,但大多数情况下,我将我的枚举与它们相关的类放在一起,而不是嵌套它们:

It probably just boils to personal taste, but most often I put my enums along with the class that they are related to, without nesting them:

public enum SomeType { } 
public class Foo { }

我多次尝试将它们嵌套(我们当然是在谈论公共枚举),但命名问题并不值得,例如:

I was tempted many times to have them nested (we're talking about public enums of course), but the naming issues weren't worth it, for example:

class Foo
{
    public enum Enumeration { }
}

然后我可以在 Foo 类之外使用这样的枚举,例如:Foo.Enumeration,但是下面的声明(在相同的命名空间中):

Then I can use such enum outside of Foo class, as: Foo.Enumeration, but following declaration (in same namespace):

enum FooEnumeration { }
class Foo { }

给出类似的结果,因为您不必输入."当您引用枚举时:FooEnumeration.此外,后者允许您这样做:

gives similar result as you just don't have to type '.' when you are referencing enum: FooEnumeration. Moreover, the latter allows you for this:

class Foo
{
    public FooEnumeration Enumeration { get; set; }
}

这会导致前面提到的命名冲突.

which would cause aforementioned naming conflicts in previous case.

总结

在使用具有强大 GoTo 功能的 IDE 时,在我看来,命名问题远比枚举定义的物理"本地化重要得多.

When using IDE with powerful GoTo capabilities, it seems to me that naming issues are far more important than 'physical' localization of the enum definition.

这篇关于枚举的单独类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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