Work Habits

By Haemoglobin
7/24/2010 (revision 10)

alt text

Model-View-Controller

alt text

  • Controller is responsible for determining which View is displayed in response to any action.
  • Differs from MVP where actions route through the View to the Presenter.
  • Every action in the View correlates with a call to a Controller along with an action.
  • In the web each action involves a call to a URL on the other side of which there is a Controller who responds. Once that Controller has completed it's processing, it will return the correct View.
  • The View does not directly bind to the Model. The view simply renders, and is completely stateless.
  • The View usually will not have any logic in the code behind. This is contrary to MVP where it is absolutely necessary as if the View does not delegate to the Presenter, it will never get called.

Recommended for Web Applications

ASP.NET MVC Sequence Diagram

alt text

ASP.NET Web Forms vs MVC

alt text

  • ASP.NET MVC uses what's known as a "Front Controller" pattern where the web server selects the appropriate controller based on the HTTP request.
  • Web Forms is a model based on the "Page Controller" pattern.

Model-View-Presenter

  • Extends from MVC, the controller is replaced with a presenter.
  • Presenter contains the the UI business logic for the View. All invocations from the View delegate directly to Presenter. The Presenter is also decoupled directly from the View and talks to it through an interface. This is to allow mocking of the View in a unit test.
  • MVP tends to be a very natural pattern for achieving separated presentation in Web Forms. The reason is because the View is always created first by the ASP.NET runtime.
  • Web applications can utilise the Web Client Software Factory to implement MVP. Reserved for large scale and durable projects, ASP.NET MVC generally recommended however.
    • Comparing MVC to WCSF. Hint: If using standard ASP.NET (with postbacks and standard controls), consider using WCSF to implement the MVP architecture, otherwise go with MVC.
  • Windows applications can utilise the Smart Client Software Factory or the MVC# Framework for implementing MVP.
  • Useful when wanting to re-use common presentation logic for multiple GUI's (i.e WPF, Silverlight client, and Windows Form Client for the same application).

Two primary variations

Passive View:

  • The View is as as dumb as possible and contains almost zero logic.
  • The Presenter is a middle man that talks to the View and the Model. The View and Model are completely shielded from one another.
  • The Model may raise events, but the Presenter subscribes to them for updating the View.
  • In Passive View there is no direct data binding, instead the View exposes setter properties which the Presenter uses to set the data. - All state is managed in the Presenter and not the View.
    • Pros: Maximum testability surface; Clean separation of the View and Model
    • Cons: More work (for example all the setter properties) as you are doing all the data binding yourself.

Supervising Controller:

  • The Presenter handles user gestures.
  • The View binds to the Model directly through data binding. It's the Presenter's job to pass off the Model to the View so that it can bind to it.
  • The Presenter will also contain logic for gestures like pressing a button, navigation, etc.
    • Pros: By leveraging databinding the amount of code is reduced.
    • Cons: There's less testable surface (because of data binding), and there's less encapsulation in the View since it talks directly to the Model.

alt text

Presentation Model

  • In this pattern there is no Presenter. Instead the View binds directly to a Presentation Model.
  • The Presentation Model is a Model crafted specifically for the View. This means this Model can expose properties that one would never put on a domain model as it would be a violation of separation-of-concerns.
  • The Presentation Model binds to the domain model, and may subscribe to event coming from that Model.
  • The View then subscribes to events coming from the Presentation Model and updates itself accordingly.
  • The Presentation Model can expose commands which the view uses for invoking actions. The advantage of this approach is that you can essentially remove the code-behind altogether as the PM complete encapsulates all of the behaviour for the view.

Recommended for WPF Applications

  • Presentation Model works extremely well in WPF due to WPF's inherent and declarative 2-way databinding capabilities.
  • In WPF, this is called Model-View-ViewModel.

Snippets from Problems and Solutions with Model-View-ViewModel:

  • When you have a large and complex domain model, it’s almost always beneficial to introduce a ViewModel layer.

  • On the other hand, sometimes the domain model is simple, perhaps nothing more than a thin layer over the database. The classes may be automatically generated and they frequently implement INotifyPropertyChanged. The UI is commonly a collection of lists or grids with edit forms allowing the user to manipulate the underlying data. The Microsoft toolset has always been very good at building these kinds of applications quickly and easily.

    If your model or application falls into this category, a ViewModel would probably impose unacceptably high overhead without sufficiently benefitting your application design.

Unlike the Presenter in MVP, a ViewModel does not need a reference to a view. The view binds to properties on a ViewModel, which, in turn, exposes data contained in model objects and other state specific to the view. The bindings between view and ViewModel are simple to construct because a ViewModel object is set as the DataContext of a view. If property values in the ViewModel change, those new values automatically propagate to the view via data binding. When the user clicks a button in the View, a command on the ViewModel executes to perform the requested action. The ViewModel, never the View, performs all modifications made to the model data. The view classes have no idea that the model classes exist, while the ViewModel and model are unaware of the view. In fact, the model is completely oblivious to the fact that the ViewModel and view exist. This is a very loosely coupled design, which pays dividends in many ways, as you will soon see.

In a sense, Views and unit tests are just two different types of ViewModel consumers. Having a suite of tests for an application's ViewModels provides free and fast regression testing, which helps reduce the cost of maintaining an application over time.

In addition to promoting the creation of automated regression tests, the testability of ViewModel classes can assist in properly designing user interfaces that are easy to skin. When you are designing an application, you can often decide whether something should be in the view or the ViewModel by imagining that you want to write a unit test to consume the ViewModel. If you can write unit tests for the ViewModel without creating any UI objects, you can also completely skin the ViewModel because it has no dependencies on specific visual elements.

Lastly, for developers who work with visual designers, using MVVM makes it much easier to create a smooth designer/developer workflow. Since a view is just an arbitrary consumer of a ViewModel, it is easy to just rip one view out and drop in a new view to render a ViewModel. This simple step allows for rapid prototyping and evaluation of user interfaces made by the designers. The development team can focus on creating robust ViewModel classes, and the design team can focus on making user-friendly Views. Connecting the output of both teams can involve little more than ensuring that the correct bindings exist in a view's XAML file.

*This page includes snippets from [StackOverflow][14]*

Comments

Powered by BlogEngine.NET 1.6.1.0 | Design by styleshout | Enhanced by GravityCube.net | 1.4.5 Changes by zembian.com | Adapted by HamishGraham.NET
(c) 2010 Hamish Graham. Banner Image (c) Chris Gin