Monday, July 20, 2009

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

Silverlight 3.0 – TechEd on Roads

I presented “What's new in silverlight 3.0” at Teched, Delhi on roads and got overwhelming response for it. Actually there was two events. The venue for the first event was Microsoft office, Gurgaon, Delhi. I must admit i had to change gears because most of the attendees were new to “Silverlight” itself; and i was introducing them to Silverlight 3.0 version!!

So i was back to questions like

  • Why Silverlight?
  • What silverlight means to
    • Developer
    • Architect
    • Manager
    • End user

It did generated lots of interest and my deal on capturing user attention by hatching on to the psychology made it home to most of the users. Examples i gave was as simple as making a grid rotate (flip) to use the same screen space to display more data. OR blurring the grid so that user can concentrate on more relevant information on screen…

Both of the above were, interestingly speaking examples of new features of SL 3.0…

There was also this interesting bit about “what it takes to move to SL platform” and concerns like “I felt lots of resistance from developers in my team while moving away from ASP .net”

It took them a moment before that they realized that SL is actually easy to work on and most importantly its the same C#, VB.net model  developers leverage on. The results they get

  • looks awesome (UI is simply great)
  • Works on every browser and just the same(no more coding for each browser and its version)
  • coding actually is easy

Getting the resistance out is just a matter of fact to demonstrate “how convenient it is” to code in silverlight and the goodies (best practices) are just there out-of-the-box.

Attached the the presentation deck along with the demo.

the best part for me is the compliment from Bijoy (who himself is renowned Microsoft evangelist) on my comment ‘Good products need good evangelists’

Technorati Tags: