This is an excerpt from, In the Box – MVVM Training that is posted on the Visual Studio Gallery.
Have a great day,
Karl
The gold standard for learning and describing design patterns is the book, Design Patterns: Elements of Reusable Object-Oriented Software.
Below, I present the MVVM pattern using the chapter format, section titles defined in the book.
Model-View-ViewModel
UI Design
Provide a clean separation of concerns between the user interface controls and their logic.
MVVM, M-V-VM and ViewModel
Development languages like Visual Basic (1-6), Microsoft Access, .NET Windows Forms, WPF, Silverlight and Windows Phone 7 provide a default experience that leads a developer down the path of dragging controls from a Toolbox to a design surface then writing code in the form's code-behind file. Many software companies and consultants have successfully delivered applications authored using these languages to customers.
As these applications grew in size and scope and were modified over-time, complex maintenance issues began to surface. Some of these issues were limitations of the language; others resulted from the lack of supporting infrastructure like source control or test frameworks. Some were the result of application logic being located in code-behind files and the resulting tight coupling between the UI form controls and the code-behind that increased the cost to make form modifications.
The four top reasons for using the MVVM pattern in Microsoft XAML applications are:
Let's quickly examine a common scenario found in forms over data applications and see how the MVVM pattern is used.
Our application requires a data entry form that allows a customer entity to be displayed, saved and or deleted.
The MVVM pattern addresses separation of concerns by defining that UI application logic be located in a view model class as opposed to the view's code-behind file. It leverages the rich data binding stack of the XAML platform to expose the view model to the view's UI controls through the view's DataContext property. A by-product of the separation of concerns is the enablement of a clean developer-designer workflow. Designers are now emancipated to modify the UI, change or rename controls to suite their requirements without breaking the application. Application testability has been increased because the view model takes its dependencies in the form interfaces, allowing the view model to be individually tested in isolation.
The above UML diagram shows:
CustomerViewModel exposes two ICommand properties that the view's UI controls which support commands can bind to
Use the MVVM pattern when developing on Microsoft XAML platforms.
Components communicate through data bindings and event notification. Additionally, the view can use behaviors to invoke public methods on the view model.
The model is depicted as an entity and the application domain.
The view model is the view's DataContext.
The MVVM pattern has the following benefits and liabilities:
Consider the following when implementing the MVVM pattern:
The below XAML snippet shows wiring up the view model in XAML. Remember, view models wired up like this must have a default empty constructor.
<Window xmlns:local="clr-namespace:Acme.MVVM" x:Class="Acme.MVVM.MainWindow"> <Window.DataContext> <local:MainWindowViewModel /> </Window.DataContext>
The below PublisherView shows how property injection can be used to wire up the view model.
public partial class PublisherView : UserControl { [Dependency] public PublisherViewModel ViewModel { get { return this.DataContext as PublisherViewModel; } set { this.DataContext = value; } } public PublisherView() { InitializeComponent(); } }
The below App class shows how a view model is added to a view during construction. This sample pattern can also be automated by authoring an IOC container extension to create the view and view model combination and wire them up as illustrated below.
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { IUnityContainer container = new UnityContainer(); container.RegisterType<IDialogService, ModalDialogService>( new ContainerControlledLifetimeManager()); container.RegisterType<IEventRepository, EventRepository>( new ContainerControlledLifetimeManager()); MainWindow window = container.Resolve<MainWindow>(); window.DataContext = container.Resolve<MainWindowViewModel>(); window.Show(); } }
The below XAML snippet shows the view Button controls binding to properties on their DataContext. The C# code snippet shows the ICommand properties that the view is binding to.
<StackPanel Orientation="Horizontal" Grid.Row="5" Grid.ColumnSpan="2"> <Button Content="New" Command="{Binding Path=NewCommand}" /> <Button Content="Save" Command="{Binding Path=SaveCommand}" /> <Button Content="Delete" Command="{Binding Path=DeleteCommand}" /> </StackPanel>
public ICommand DeleteCommand { get { return new RelayCommand(DeleteExecute, CanDeleteExecute); } } public ICommand SaveCommand { get { return new RelayCommand(SaveExecute, CanSaveExecute); } } public ICommand NewCommand { get { return new RelayCommand(NewExecute); } }
There are numerous XAML applications around the world that use the MVVM pattern. This pattern is heavily promoted by the XAML platform community, Microsoft MVP's and respected technology leaders.
Expression Blend and the Visual Studio 2010 Designer both use the MVVM pattern.
The following links are from Martin Fowlers web site.