سؤال

C# MVC project, where is the best place to put an Enum that is used in a number of classes? Please read on.

I have an enum that is required in a domain model. Since I'm using view models based on this domain model, the enum is also required in the view models.

The class for the domain model is (appropriately) in the Model/ folder. Classes for the view models are in the ViewModel/ folder (which I created). So we are looking at more than one namespace, no drama, just something to note.

Initially I was going to create a static class, and put this enum and any other globally accessed variables in here. But I've read, it's best (i.e. easier) to put the enum out of a class, to save you from referencing the class name each time you want to use the enum, e.g. classname.myenum. Fair enough, agreed.

Is there a best practice on where to create such a class file, and a appropriately named namespace? My thoughts, since the enum was initially required in the domain model, the lowest level, create a class file in the Model folder. And reference this namespace.enum for my domain class and view models.

Or, what about the Global.asax file? Is this a good place to put global variables?

هل كانت مفيدة؟

المحلول

Or, what about the Global.asax file? Is this a good place to put global variables?

Actually, enum is not a variable, it's a type.

I had the same question few days ago. I had a model, and viewmodel, and I need a way to share enums between them.

I have set of folders in ASP.NET MVC solution for this kind of goals. And I put all of my enums to MyProjectName.Infrastructure.Common.Constants

I think it's quite wise, because with Constans namespace you're letting know other developers that these types are constants. With Common namespace you're telling others that these things are shared.

You can find some examples on Infrastructure namespace using here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top