Simple Mvvm Toolkit Version 2.0: Better and Even Easier to Use

I have just released version 2.0 of the Simple Mvvm Toolkit on CodePlex, NuGet and the Visual Studio Extensions Gallery.

smvvm-vsext

Now all you have to do to get it is open Visual Studio and select Extensions Manager from the Tool menu, then search for “Simple Mvvm Toolkit” and click the Download button.  Launch SimpleMvvmInstaller.exe and you’ll get the whole shebang: binaries, project and item templates, code snippets, samples and source code, copied to the SimpleMvvmToolkit directory in your Program Files folder.  The ReadMe.txt file lists all the prerequisites you’ll need to install before using the toolkit. The installer will even add the toolkit to Visual Studio’s referenced assemblies, so that it appears in the list of assemblies when you show the Add Reference Dialog.

smvvm-addref

Visual Studio integration is nice, but the killer feature of v2 is the new Visual Studio project templates. You heard right, the installer not only gives you the item templates and code snippets you had in v1, but it also provides four project templates: Silverlight, Windows Phone, WPF and RIA Services.  Each of these gives you a ready-made Visual Studio project that references the toolkit and includes Models, Views, ViewModels, ServiceAgents, and a ViewModelLocator.  All you have to do is open the included ReadMe.txt file and follow the simple instructions, using each of the provided classes as examples.  To get started just open Visual Studio, select File, New Project, then click on the Mvvm category beneath Windows (for WPF), Silverlight, or Silverlight for Windows Phone.

smvvm-proj

If you select, for example, the SimpleMvvmSilverlight template, you’ll get a brand new project with the following folder structure.

smvvm-sl

This is nice, but the amazingly awesome project template is SimpleMvvmRiaServices.

smvvm-ria

Selecting this template will give you three projects: a Silverlight app, a Web host (the Silverlight and Web projects have a RIA Services link), and a Test project.  [This, by the way, took a lot of effort to pull off.]  Press F5 and voila!  You have a fully functional Silverlight app with WCF RIA Services that uses the Simple MVVM Toolkit, including ViewModel-driven navigation with the MessageBus, event-based modal dialogs, entity persistence with CRUD operations, and unit testing with MEF-based dependency injection. Wow! From this point all you have to do is follow instructions in the ReadMe.txt files in each of the three projects and replace supplied classes with your own using the SimpleMvvm item templates and code snippets.  [NOTE: I intend to publish a screen cast showing you precisely how to do this.]

smvvm-riarun

If you set the Test project as the startup and hit F5, you’ll get a fully functional unit test application.  This is enabled by the supplied InjectedViewModelLocator (found in the Locators folder) and the ServiceAgentExport attribute placed on real and mock service agents.

smvvm-testrun

As you can see, v2 of the toolkit offers a lot in the way of productivity and ease-of-use.  But if you were to peek under the covers you’d discover even more simple goodness.  First, I have removed the use of a dictionary in the ViewModelLocator item templates and instead create ViewModels on-demand in property getters.  This involves a breaking change in the way you set the DataContext in Views.  There is no longer the need to insert square brackets around the ViewModel property in the Path.  This allows the View to control the lifetime of the ViewModel, avoiding a potential memory leak, but it also results in a lot less code in the locator when you use the mvvmlocator, mvvmlocatornosa (no service agent required) or mvvminjectedlocator (injected service agents) code snippets.

DataContext="{Binding Source={StaticResource Locator}, 
    Path=ItemListViewModel}"

In addition, the MessageBus is now implemented using a leak-proof eventing model, which removes the requirement to un-register for messages.  To simplify messaging even further, the ViewModelBase class now has two easy methods you can call: RegisterToReceiveMessages and SendMessage. (The only other breaking change in v2 is that you no longer use the MessageBus class directly.) Note that inter-ViewModel communication using the MessageBus is only required if you wish to perform navigation based on logic in a ViewModel, such as a Save method.

public class MainPageViewModel : ViewModelBase<MainPageViewModel>
{
    public MainPageViewModel()
    {
        this.RegisterToReceiveMessages(MessageTokens.Navigation, 
            OnNavigationRequested);
    }
}
public class CustomerViewModel : ViewModelBase<CustomerViewModel>
{
    public void Save()
    {
        SendMessage(MessageTokens.Navigation, 
            new NotificationEventArgs(PageNames.Home));
    }
}

Lastly, v2 adds Parts 2 and 3 to the Main sample application.  Part 2 walks you through features such as dependency injection, unit testing, messaging, property associations and long-running async operations.  Part 3 simply ports the application to WCF RIA Services and adds methods for saving batch changes (inserts, updates and deletes) in a single round-trip to the service.

Speaking of unit testing, one other thing to note is that the toolkit contains an extension method that makes it easier to perform async unit testing with the Silverlight Unit Testing Framework that ships as part of the Silverlight Toolkit.  Basically it adds a timeout to the EnqueueConditional method so that tests that fail don’t go on forever.

So there you have it! Version 2 of the Simple MVVM Toolkit is more robust and is even easier to use: an automated installer, Visual Studio project templates (including RIA Services), simplified locator and messaging APIs that protect you from memory leaks, and expanded end-to-end sample applications. Sweet.

About Tony Sneed

Married with three children.
This entry was posted in Technical and tagged . Bookmark the permalink.

17 Responses to Simple Mvvm Toolkit Version 2.0: Better and Even Easier to Use

  1. JeffW says:

    Tony, thanks for this great toolkit. Any plans to support us VB.NET progammers? I was hoping it would be a simple matter of adding a reference to your library and code away. However, I get a ‘not CLS-compliant’ when deriving from any of your classes. Is there anything I can do to remove this warning, or will the assemblies need to be modified and recompiled to make things work correctly. I could probably do this myself, but of course your next release would require me to fix it again.

    Also, I have manually converted your code snippets and templates (a little time consuming) for use with VB.NET. Any chance of providing these out-of-the-box, or at least documenting the ones that have changed so I don’t have to analyze/modify all of them?

    Thanks

  2. Jeff says:

    Tony, thanks for this great toolkit. Any plans to support us VB.NET progammers? I was hoping it would be a simple matter of adding a reference to your library and code away. However, I get ‘not CLS-compliant’ warnings whenever I try to inherit/implement any of your classes. I am quessing the assemblies would need to be modified and recompiled in order to make things work correctly?

    Also, I have manually converted your code snippets and templates (a little time consuming) for use with VB.NET. Any chance of providing these out-of-the-box, or at least documenting the ones that have changed so I don’t have to analyze/modify all of them?

    Thanks for your continued work on this toolkit.

  3. davis says:

    Hi Tony, I had already started a VS 2010 RIA sevices C# project, and now I’m trying to pull your stuff in to use it. It compiles and loads fine, but I also get the warnings about all your classes being “not CLS-compliant”. Is there something I need to do to resolve this?

  4. Tony Sneed says:

    @davis: You must be one of our VB.NET users! 🙂 Please see the comment above that has a link to the latest source code, in which I added the CLS compliance attribute. You can download and use it. I plan to put out another release of the toolkit in the next couple of weeks. So stay tuned …

  5. Chris says:

    Hi Tony – Thanks for the great toolkit. I am still very new to this but I wonder if you would be able to help point me in the right direction with a problem I’m having with a detail view when I edit an item on the list? I haven’t really changed your code structure at all, just the data being passed through.

    ListView: – editItemButton_Click
    I have a model.SelectedItem that I pass to the constructor of the detailViewModel. One of the properties of this item is a collection of child items which I can see fine.

    I now pass that detailviewmodel to the constructor of a DetailView. However, when I debug setting the DataContext of the view I notice the collection is empty. The other properties are set fine but not the collection?

    Aplogies if this is not really the place to post this but just wondered if I was missing something obvious? Thanks.

  6. Vipul Arwade says:

    Hi Tony,

    We are using Simple MVVM in our project.We have added ViewModel which are inherehing from ViewModelBase class from SimpleMVVM fremawork.We need to add one ViewModel which will be base for all view models.But all view model allready inheriting from ViewModelBase.Please tell us how to add BaseViewModel for common functionality.

    • Tony Sneed says:

      I would suggest having your base class inherit from ViewModelBase, and then inheriting the ViewModels from your base class.

      Cheers,
      Tony

      • vipularwade says:

        Hi Tony,

        There are view models in projects which are inheriting from ViewModelDetailBase and ViewModelBase.
        I need to create one base view model which contain some common functionalities for all view models.Becasue of limitatoin of mulitple inheritance ,not able to create base view model.For these,is there need to create two base view models which are inheriting from ViewModelDetailBase and ViewModelBase.
        Please suggest how to create base view model for detail view model and non detail view model.

      • Tony Sneed says:

        I would suggest putting common functionality in a “helper” class used by both of your base classes. That’s basically what the toolkit does to overcome the limitation of no multiple inheritance. One of your base classes would inherit from ViewModelBase and the other from ViewModelDetailBase – each would make use of functionality in a third class, which is the helper.

  7. andy says:

    Tony the toolkit creates a folder structure View-Model-ViewModel in the same silverlight project. However in production grade app, I need to have separate projects for each. How to convert the folder structure into different project structure? Thanks

    • Tony Sneed says:

      It doesn’t really matter where you want to locate various elements. Just create silverlight class library projects for each group. You can keep ViewModels and the locator in a separate project, for example, and reference it from the main project. If you do that, make sure you add the assembly to the namespace in the xaml of each view, so that it knows where to find the locator.

  8. UragX says:

    Hi Tony,

    how can I Implement a navigation framework on WPF application with your Simple MVVM Toolkit?

    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s