Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should enums live in an MVC project structure?

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

like image 817
Acaz Souza Avatar asked Aug 09 '11 18:08

Acaz Souza


People also ask

Where should enum be placed?

Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.)

What is enum in MVC?

The MVC framework sees the enum value as a simple primitive and uses the default string template. One solution to change the default framework behavior is to write a custom model metadata provider and implement GetMetadataForProperty to use a template with the name of "Enum" for such models.

Where should enums be defined Java?

Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon. Note: The constructor for an enum type must be package-private or private access.

Is enum a class C#?

An enum is a special "class" that represents a group of constants (unchangeable/read-only variables).


2 Answers

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

like image 162
cyrotello Avatar answered Sep 19 '22 02:09

cyrotello


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.

like image 38
Domenic Avatar answered Sep 19 '22 02:09

Domenic