Managing Models in ASP.NET MVC


Peter Vogel looks at the least important component of the ASP.NET MVC, the Model, and goes on to discuss a strategy for integrating the model, the controller and the view.

One of the key design criteria for a controller is that it have no business logic and no display logic. The controller has two (and only two) responsibilities: Pick the View and gather the data from the model and pass it to the View. The View's responsibility is to display the data to the user and gather the user's input.

So the model does everything else: moving data in an out of the database, handling business rules, and more. However, in an ASP.NET MVC application, it's the least important component because the model has the least to do with the Web application. A controller should be able to work with any model and it should be possible to plug any model into an ASP.NET MVC controller. As with the business objects accessed by an ASP.NET application, there are no requirements imposed on the model's objects (unlike an ASP.NET MVC controller which must, for instance, implement the IController interface and follow a specific naming convention).

That doesn't mean that there aren't some things to consider when building an ASP.NET MVC application.

When it comes to the business layer, my first inclination these days is to use Entity Framework and extend the entities that it returns with Partial Classes. The next obvious step is to use WCF Services (as I discussed in an earlier column in conjunction with AJAX). A controller could call a WCF service and receive a business entity in return which the controller could then pass onto the View. This would work very well, for instance, for a page or form that supports maintaining a lookup table.


Reference: http://visualstudiomagazine.com/articles/2010/08/31/managing-models-in-aspnet-mvc.aspx


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: