C. Ross
C. Ross

Reputation: 31858

ASP.NET MVC - Strongly typed view model, where does it belong?

I'm trying to create a strongly typed view model as John Sheehan suggests here. Where should it go? I can make arguments to myself for Model, View, and Controller.

Upvotes: 3

Views: 860

Answers (4)

Max Toro
Max Toro

Reputation: 28618

It can go wherever you want it to go, why do you need someone to tell you where to put a class?

A lot of people have the wrong idea that, unless you put your classes inside some specific directory grouped by functionality, things will not work. This might be true with other frameworks, but with ASP.NET MVC it's not true. Code is compiled to assemblies.

Upvotes: 0

ten5peed
ten5peed

Reputation: 15890

If have them in my Domain project in a PresentationModel directory and like @Seth Pretry-Johnson, I have them in separate Controller directories.

This is my overall structure of a project:

  • Website Project
    • Controllers
    • Views
    • Etc
  • Domain Project
    • Models
    • Repositories
      • Abstract
    • Services
      • Abstract
    • PresentationModels
      • Home
      • User
      • Etc
  • DataAccess Project
    • Repositories

HTHs (and doesn't raise more questions.. ;-),
Charles

Upvotes: 3

Seth Petry-Johnson
Seth Petry-Johnson

Reputation: 12085

It should go in the "Models" directory of the web app. ViewModels are by definition specific to one or more views and therefore belong in the web app, not the core.

You could define them in the controller that uses them, but this doesn't scale. Same with defining the class in the view code. Even though one-class-per-file means more files, its easier to find code and easier to maintain.

I'll often create a subfolder for each controller, so I end up with things like Web.Models.Foo.BarViewModel.

Upvotes: 4

Jarrett Meyer
Jarrett Meyer

Reputation: 19583

I put the actual model class in the Models folder.

/Controllers
/Models
    /Entities
    /Mappings
    /ValueTypes
    /ViewModels

Something like that. I'm a big fan of the Fluent NHibernate.

Upvotes: 0

Related Questions