concept presentation model in category c#

appears as: presentation model, presentation models, The presentation model, presentation model
ASP.NET MVC 2 in Action

This is an excerpt from Manning's book ASP.NET MVC 2 in Action.

Some screens are more complex than a single table. They may feature multiple tables and additional fields of other data: images, headings, subtotals, graphs, charts, and a million other things that complicate a view. The presentation model solution scales to handle them all. Developers can confidently maintain even the gnarliest screens as long as the presentation model is designed well. If a screen does contain multiple complex elements, a presentation model can be a wrapper, composing them all and relieving the markup file of much complexity. A good presentation model doesn’t hide this complexity—it represents it accurately and as simply as possible, and it separates the data on a screen from the display.

It’s common to have a class whose sole responsibility is to formulate the presentation model. Doing the work of building a presentation model in application code is better than doing that work in the view. The view is convoluted enough as it is, and it’s focused on HTML and style. A separate class that creates the presentation model can be easily tested, programmed, and maintained.

It’s also best not to create the presentation model in the controller. The controller is busy deciding which view to render and coordinating these other efforts. Listing 2.2 offers a simplistic look at how a controller might send the presentation model to the view.

Listing 2.2. A controller action preparing the presentation model
ASP.NET MVC in Action with MvcContrib, NHibernate, and More

This is an excerpt from Manning's book ASP.NET MVC in Action with MvcContrib, NHibernate, and More.

Listing 1.11. Passing in the presentation model to the View method

This is where the presentation model shows its value. The presentation model is responsible for transforming the domain model into a representation that is useful for the presentation layer, namely, the controller and view. Whereas the domain model is an n-dimension object graph that accurately represents the real world, the presentation model takes these objects and projects them into a flatter model that can easily be represented on a graphical screen.

The presentation model can be many things. Ultimately, it is an object model that serves a particular screen, not the entire domain. This object model can be populated in isolation, or it can take responsibility to populate itself when a domain object is passed into the constructor. The presentation model goes in the /Models folder in an ASP.NET MVC Framework application. The presentation model is part of the presentation layer and should not be referenced by the rest of the application. Typically a complex screen will require a presentation model object graph.

Let’s revisit our conference listing example. It would not be appropriate for a controller to pass a Conference object to the view and hope the view knows how to traverse the object graphic in order to render the correct information. This is too much responsibility for the view. The controller needs an object that it can send to the view so that there is only one way for the view to render the object or structure of objects. If the view has a decision to make, we have introduced the possibility for a functional bug in the part of the code that is the most difficult to test. We want to pull all decisions back into the heart of the application where they can easily be tested. For this, we’ll have ScheduleController use a new object in the presentation model called ScheduleForm. The controller will map a conference into a ScheduleForm instance by leveraging IScheduleMapper. See figure 2.4 for the ScheduleForm structure.

Figure 2.4. The presentation model contains classes specific to a particular screen. Logic that decides how to structure the domain model for presentation on a screen can be factored into presentation model’s classes.