0

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?

4 Answers 4

6

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.

3
  • 1
    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. Jun 2, 2009 at 17:13
  • 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
    Jun 2, 2009 at 21:22
  • My View Models are generally completely new classes that make use of the data models as properties and so forth on the view models. Jun 12, 2009 at 17:30
2

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.

2
  • Yes, I mean that my model objects are exposed via WCF services.
    – Richard Ev
    Jun 1, 2009 at 14:11
  • 1
    @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. Jun 1, 2009 at 14:39
0

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.

2
  • 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)? Jun 1, 2009 at 14:06
  • I'll check again, but yes the other project is referenced -- they don't have the same root namespace, however.
    – tvanfosson
    Jun 1, 2009 at 14:15
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.