枚举应该位于 MVC 项目结构中的什么位置?

发布于 2024-11-28 14:24:42 字数 389 浏览 4 评论 0原文

我正在使用 .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:

enter image description here

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

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

发布评论

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

评论(4

π浅易 2024-12-05 14:24:42

您真正应该关心的是枚举的命名空间

无论您的类文件存在于解决方案中的哪个位置,您的代码都将依赖于命名空间。我想您可能需要一个像这样的命名空间: Questiona2011.Enums 。将 Enum 类绑定到 Models 命名空间并不是一个好主意 - 并不是说​​它不能完成,而是有时视图可能需要与您的枚举进行交互。所以我倾向于给我的枚举一个单独的命名空间。

您不一定需要为类文件创建一个文件夹...如果您愿意,您可以保留在根目录中 - 真正的因素是命名空间。

因此,创建一个具有如下命名空间的类:

using System;

namespace Questiona2011.Enums
{
    public enum Score
    {
        One = 1,
        Two = 2,
        .
        .
        .
        Ten = 10
    }
}

话虽如此,我只是将类文件放在 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 the Models 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:

using System;

namespace Questiona2011.Enums
{
    public enum Score
    {
        One = 1,
        Two = 2,
        .
        .
        .
        Ten = 10
    }
}

Having said that, I'd just drop the class file in the Models folder. :)

影子是时光的心 2024-12-05 14:24:42

听起来你有一个值对象。我会将它放在域中放置其他值对象的同一位置,这实际上取决于您的文件夹结构。绝对在模型文件夹中,但如果您要细分模型文件夹,则取决于您的操作方式。您有“问答”子文件夹吗?也许它就在问题旁边。或者您有 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.

聽兲甴掵 2024-12-05 14:24:42

除非有更好的地方来放置它们,否则我将它们放在 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.

﹏雨一样淡蓝的深情 2024-12-05 14:24:42

如果您的项目变得足够大,需要担心组织问题,那么您应该考虑创建一个新项目,该项目只是适用于您的应用程序类型的 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.

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