MVVM == Model View ViewModel Pattern is a MUST follow pattern in Silverlight (WPF). here is why…

Problems it solves:

•Tight coupling of layers (ex a query in the UI)

•Unit testing is hard/impossible, only functional testing is possible

•Assures codes still works correct after a change

•ViewModel

•Provides View with data model and behavior

•View will bind to ViewModel

•Wraps data access entirely

In light of new technologies like RIA services (which provides CRUD) operations out of the box it is imperative that users are tempted to overlook MVVM for sake of simplicity!!! However, the goodies MVVM brings along holds good in RIA services well… So what the roadmap to have MVVM , silverlight and RIA services???? I havnt found much on google (& BING!!!) but here is my humble take on that.

I plan to write more in my forthcoming blog but here is one example using nInject.

nInject is lightweight DI framework and is helpful in injecting VIEWMODEL right into XAML code!!! here is how…

   1: Title="Home"
   2:   DataContext="{Binding Path=HomeViewModel, Source={StaticResource serviceLocator}}"
   3:   Style="{StaticResource PageStyle}">

Notice … the datacontext (VIEW MODEL) is injected using ServiceLocator!! the service locater is itself a resource defined in APP.xaml

   1: Application   
   2:   x:Class="SilverlightApp.App"
   3:   xmlns:local="clr-namespace:SilverlightApp.ViewModel"
   4:   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   5:   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   6:  
   7:   <Application.Resources>
   8:   
   9:     <ResourceDictionary>
  10:       <ResourceDictionary.MergedDictionaries>
  11:         <ResourceDictionary Source="Assets/Styles.xaml"/>
  12:       </ResourceDictionary.MergedDictionaries>
  13:     </ResourceDictionary>
  14:         <local:ServiceLocator x:Key="serviceLocator" />
  15:     </Application.Resources>
  16:  
  17: </Application>

and Ninject code for Service Locater goes like…

   1: namespace SilverlightApp.ViewModel
   2: {
   3:     using Ninject.Core;
   4:     public class ServiceLocator
   5:     {
   6:         private static readonly IKernel kernel;
   7:  
   8:         static ServiceLocator()
   9:         {
  10:             if (kernel == null)
  11:             {
  12:                 kernel = new StandardKernel(new Module());
  13:             }
  14:         }
  15:  
  16:         public HomeViewModel HomeViewModel
  17:         {
  18:             get
  19:             {
  20:                 return kernel.Get<HomeViewModel>(); 
  21:             }
  22:         }
  23:  
  24:         public static T Get<T>()
  25:         {
  26:             return kernel.Get<T>();
  27:         }
  28:     }
  29: }

 

The commands are taken care by the SLExtensions. here is how…

I will be using and blogging about the following DI … Please feel free to write more… if you have anything to add..

1. Nikhil Silverlight FX… This is really good stuff; the only thing which is “does NOT” goes down well (to most of developers) is use of customized controls. Having said that i must say, personally, i love this approach and how Nikhil keep updating it with newest stuff!!!

2. CAB(Prism): I personally think it is more opt for WPF. Having said that it also follows “injection through interface” paradigm.. which sometimes introduces too much hassles…

3. MEF. Latest from Microsoft. Very promising, however yet to mature as a tool.

4. Caliburn: Excellent tool. I found it at codeplex…

 

My plan is to retest each of the above wrt SL3.0, RIA services abd MVVM in context abd post results on this blog.. and get more tractions on the results i get to..

Keep jotting your comments…

Technorati Tags: ,,