开发者

Where should enums live in an MVC project structure?

开发者 https://www.devze.com 2023-03-26 21:10 出处:网络
I\'m using .NET MVC 3 Code-First approach. My domain have a en开发者_开发知识库tity called Question, this entity have a Score property that is Byte and I want to replace that property to an Enum and n

I'm using .NET MVC 3 Code-First approach. My domain have a en开发者_开发知识库tity 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:

Where should enums live in an MVC project structure?


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消