In an MVC project structure, where should enums go?

.net asp.net-mvc domain-driven-design entity-framework enums

Question

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

1
11
8/9/2011 6:20:20 PM

Accepted Answer

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.

2
8/9/2011 6:12:23 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow