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:
Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.)
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.
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.
An enum is a special "class" that represents a group of constants (unchangeable/read-only variables).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With