枚举应该位于 MVC 项目结构中的什么位置?
我正在使用 .NET MVC 3 代码优先方法。我的域有一个名为 Question
的实体,该实体有一个 Score
属性,该属性为 Byte
,我想将该属性替换为 Enum
并将此 Enum
命名为 Score
,我可以在其中设置 0 到 10 之间的值。Enum
应该位于该结构中的什么位置?在我的 Model 文件夹中名为 Enums 的文件夹中?
更新:
这是我在Models文件夹中的项目结构:
I'm using .NET MVC 3 Code-First approach. My domain have a entity called Question
, this entity have a Score
property that is Byte
and I want to replace that property to an Enum
and name this Enum
to Score
where I can set the values from 0 to 10.
Where should Enums live in that structure? In a folder called Enums in my Model folder?
Update:
That's my project Structure in Models folder:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您真正应该关心的是枚举的命名空间。
无论您的类文件存在于解决方案中的哪个位置,您的代码都将依赖于命名空间。我想您可能需要一个像这样的命名空间:
Questiona2011.Enums
。将 Enum 类绑定到 Models 命名空间并不是一个好主意 - 并不是说它不能完成,而是有时视图可能需要与您的枚举进行交互。所以我倾向于给我的枚举一个单独的命名空间。您不一定需要为类文件创建一个文件夹...如果您愿意,您可以保留在根目录中 - 真正的因素是命名空间。
因此,创建一个具有如下命名空间的类:
话虽如此,我只是将类文件放在 Models 文件夹中。 :)
What you really should be concerned about is the namespace of your enums.
Regardless of where your class file exists in the solution, your code will rely on the namespaces. I'm thinking that you'd probably want a namespace like:
Questiona2011.Enums
. It won't be a good idea to tie the Enum classes to theModels
namespace - not that it can't be done, but sometimes the views may need to interact with your enums. So I tend to give my enums a separate namespace.You don't necessarily need to create a folder for the class file... you can keep in in the root directory if you'd like - The real factor is the namespace.
So create a class with the namespace like so:
Having said that, I'd just drop the class file in the Models folder. :)
听起来你有一个值对象。我会将它放在域中放置其他值对象的同一位置,这实际上取决于您的文件夹结构。绝对在模型文件夹中,但如果您要细分模型文件夹,则取决于您的操作方式。您有“问答”子文件夹吗?也许它就在问题旁边。或者您有 Value Objects 子文件夹吗?也许在那里。
It sounds like you have a value object. I'd put it in the same place you put other value objects in your domain, which really depends on your folder structure. Definitely in the Model folder, but if you're subdividing the model folder, it depends on how you're doing that. Do you have a Q&A subfolder? Maybe it goes there next to questions. Or do you have a Value Objects subfolder? Maybe there.
除非有更好的地方来放置它们,否则我将它们放在 Model 文件夹中。
如果你有很多枚举,你可能想要做你正在做的文件夹想法。我不认为我会称其为“枚举”,因为这不是很有描述性。
Unless there is a better place to put them, I stick them in the Model folder.
If you have a lot of Enums though, you might want to do the folder idea that you were doing. I don't think I would call it "enums" though as that's not very descriptive.
如果您的项目变得足够大,需要担心组织问题,那么您应该考虑创建一个新项目,该项目只是适用于您的应用程序类型的 DLL。
If your project is getting large enough to be concerned about organization, you should consider creating a new Project that is just a DLL for your applications' types.