• Prism Automation: Templates for implementing the MVVM pattern using the Composite Application Library

    Published by on August 15th, 2009 5:33 pm under Composite Application Guidance, Patterns and Practices, Prism, Prism-v2, Silverlight, WPF

    1 Comment

    With the help of Damian Schenkelman and Julian Dominguez, we created some Visual Studio Item Templates that provides the basic assets for implementing a new view using the MVVM pattern in Prism. These templates create the necessary files and classes for the view and viewModel parts of the pattern.

    I will explain in this post how to install and use the Prism MVVM templates. If you are not familiar with the MVVM pattern the following links are a good staring point:

    These MVVM templates aim to help you facilitate the creation of views with presenters when developing prism applications. Then we disscuss briefly some implementation considetarions.
    David Hill has recently posted the Prism Quick Start Kit which includes project templates for creating prism Modules and Shell, both for WPF and Silverlight. Damian has created code snippets for easing the creation of Commands with attached Behaviors.

    How-To: Install the Prism MVVM Templates

    Installing the templates it’s really simple!

    1. Dowloand templates and unzip them into your Visual Studio templates folder. They are C# templates so they go here on my system:
          My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#
    2. To avoid getting a warning message the first time you use them, you can close Visual Studio and run the following command (as admin) in a Visual Studio Command Prompt window:
          devenv.exe /installvstemplates

    That’s all! you should now be able see them in the New Items dialog.

    How-To: Use the Prism MVVM Templates

    Using the templates it’s also really simple (that’s the idea of using templates after all :-)).

    1. Open any prism application in Visual Studio. (Templates will work in any kind of application, but you will have to reference CAL assemblies).
    2. Right click on the project/folder where you want to place your new view and select Add / New Item…The New Item Dialog will appear.
    3. Select Prism MVVM or Prism MVVM (with interfaces) from the My Templates category.image
    4. Click Add.  This will create the View with xaml and code behind, and the ViewModel. If you selected Prism MVVM (with interfaces) template, it will additionally create interfaces for the View and ViewModel (this is highly recommended as it will ease testing).

    image

    Great, we have now the new view with it’s corresponding ViewModel.  To show the view you should register it in the appropriate region as usual. For example, using view discovery (if you didn’t used interfaces, you won’t need the first line):

    this._container.RegisterType<ICustomersViewModel, CustomersViewModel>();
    this._regionManager.RegisterViewWithRegion(RegionNames.ButtomLeftRegion, () => this._container.Resolve<CustomersView>());

    Using the above code in David’s Quickstart Solution, you can get the view shown:

    image

    Implementation considerations:

    You will notice in the implementation of the view and viewModel that:

    1. View-First approach is used.
    2. Templates can be used in WPF and Silverlight prism applications.
    3. The implementation relies on prism, as it inyects the EA to the ViewModel.
    4. A property is created in the viewModel, to demostrate databinding in the view.

    You can download the templates from here.
    kick it on DotNetKicks.com

    Tags: , , , , ,