views:

531

answers:

4

Are there any best practices that cover the places that ASP.NET MVC models should be defined?

A new ASP.NET MVC project has a nice neat Models folder for them to go in, but in a production environment they can come from other places:

  • Third party class libraries
  • WCF services

Is it acceptable for a strongly-typed view to use a class defined in such a location?

A: 

One issue that I've found is that, unless the model is defined in the web project, VisualStudio seems unable to find it when using a strongly-typed view specified in markup. My models are usually defined in a separate project and I've found that to use strongly-typed views, I need to create a codebehind so that I have a class that derives from a strongly-typed ViewPage. Then I change the markup and associate it with this class.

tvanfosson
I haven't had any of those problem. Do you reference the project correctly from your web app, and make sure that they have the same root namespace (or reference the other namespace correctly)?
Tomas Lycken
I'll check again, but yes the other project is referenced -- they don't have the same root namespace, however.
tvanfosson
+2  A: 

I don't know what you exactly mean by putting models in WCF services. If you mean using WCF services that expose the model object you need, that would work.

Regarding separate class libraries to hold your models, views and controllers, I think that's a pretty common approach and works pretty well. In fact, I believe this is really a requirement when the size and complexity of your application grows. It's a kind of physical separation of the distinct logical components in an MVC app.

Mehrdad Afshari
Yes, I mean that my model objects are exposed via WCF services.
Richard Ev
@Richard E: In that case, when you add a service reference to the project, essentially you're auto-generating model classes in an assembly, so the problem reduces to your "third party class library or not" question.
Mehrdad Afshari
+5  A: 

In just about every project I have worked on the models of ASP.NET MVC are more View Models than models in the traditional sense of the word. I have yet to have a project where I can use the same Model that I use in my data access for my View Model. There is just too much other information that needs to be displayed on most pages. So for that reason I will either store my models in the models folder or store them in a separate library with all of my other MVC specific classes.

Larry Beall
To this end, I often end up having viewdata classes - the controller grabs the appropriate models of different types and creates creates a synthesis of them in a viewdata class which is then passed to the strongly typed view.
DrJokepu
Do you define completely new view model classes that inherit from or are initialised by your data model classes, or do you use partial classes?
Richard Ev
My View Models are generally completely new classes that make use of the data models as properties and so forth on the view models.
Larry Beall
A: 

You need to import the namespace to the view page. This does not require a codebehind page.

Use the directive

<%@ import namespace='your.namespace.here' %>

immediately after the <@ Page..... directive

grahamsw