Wednesday, November 10, 2010

DataTemplate Vs. View Injection

PRISM tells us that each view needs a contracted interface, eg. IMyView which your view needs to implement.

This approach is to let us inject the views into the appropriate region (this injection usually happens in the controller). Given this guideline, what has happened to the classic MVVM approach of using DataTemplate?

Traditionally, we would have some ItemsControl to present the View-Models and define the custom DataTemplate that will define the associated view. For some, you may have noticed that when the DataContext of this ItemsControl changes, a new view is created for the new View-Model (this is sometimes an undesirable side effect).

The View Injection approach to my knowledge was derived from the Model-View-Presenter (MVP) pattern, this means that for those that has gone from MVVM to PRISM will want to keep the traditional DataTemplate to concrete view types. This is fine but as long as you are consistent in your approach.

Personally, I find that there is no need for an interface for each view because realistically you are not going to unit test the views and produce meaningful Asserts or replace any of the views through IoC any time soon.