ASP.NET MVC: Convention for Organizing ViewModels

As discussed over several MVC questions and blogposts , we know that the ASP.NET MVC project layout is very heavy.

I blindly made a subdirectory in a folder Controllers

.

This is not true.

alt text http://www.imagechicken.com/uploads/1252003002097179400.png

Question: What's the generally accepted convention about which directory your ViewModels are stored in? What are your suggestions or what is an established agreement?

+2


source to share


3 answers


I think the idea is that (View) Models should go in the Models directory (which is empty when creating a new ASP.NET MVC project).



For me personally, it makes more sense to arrange namespaces around functions instead of mechanics, but while this has nothing to do with the Model, it has some implications when it comes to controllers and views.

+5


source


I am using the Models folder along with controllers and views. The one that is empty in your project (I am not using scopes).

The "M" in the "MVC" model is in a separate assembly. The only models in the web assembly are the view / edit models.

Inside the Models folder there are usually subfolders by namespace. So I have:



Vertex.Data (build with repositories, etc.)

Vertex.Web

Controllers
  BarController
  FooController
Models
  Bar
    BarListItem
  Foo
    FooDetail
    FooListItem
Views
  Bar
  Foo
  Shared

... etc..

+11


source


I usually create a model for each view. Even if it's just an exact map of some other object in my project that I could use. This way my point of view and the required model are separate from the rest of the application. It also makes it easier to add data to the view in the future by simply extending your view model.

It takes a little more work up front and it sometimes seems like your duplicate objects, but I just prefer separation.

I store all my viewmodels in a directory of models created in an MVC project. These "models" are compared with each other with my views. I use subfolders in the Models folder when view models are becoming more than just primary data holders. The subfolders will contain all the bits and pieces needed to represent this view.

+7


source







All Articles