Silverlight 3.0, RIA services and MVVM

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: ,,
Advertisements

3 thoughts on “Silverlight 3.0, RIA services and MVVM

  1. On that note of Silverlight.FX, and customized controls … not sure which ones you’re referring to – any custom controls there are orthogonal to the view model stuff.

    That said, custom controls are key to extending the platform with additional functionality. Every control in the platform is a “custom control” with respect to the core runtime anyway… no difference.

    Like

    • Thank you Nikhil for your comments.
      I fully agree with you and do find Silverlight FX a useful platform to leverage on as far as MVVM is concerned. However, most developers in my team do find custom controls difficult to digest as far as implementing MVVM is concerned. I think, it will take some efforts at my end to convince them into it.

      I typically am a fan of how elegantly Stoaryboards are tackled in SL FX using MVVM compared to either PRISM or nInject..

      Meanwhile, i was also toying with the idea of having DI (dependency injection) built into view ; which takes care of orthogonality aspects…
      Please keep sharing your thoughts…

      Like

  2. Thank you Nikhil for your comments.
    I fully agree with you and do find Silverlight FX a useful platform to leverage on as far as MVVM is concerned. However, most developers in my team do find custom controls difficult to digest as far as implementing MVVM is concerned.
    I think, it will take some efforts at my end to convince them into it.
    Meanwhile, i was also toying with the idea of having DI (dependency injection) built into view ; which takes care of orthogonality aspects….

    Like

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