MVVM, Data Channel, MEF, WCF RIA Services and ViewModel Locator – Silverlight 4.0

In the Aspen project I decided to use a ViewModel locator to find the ViewModel a View should use, I also use MEF for composition of parts. In this post I’m going to write about the MVVM implementation we use in the Aspen project, but first about the Data Channel used in Aspen.

Data Channel

The Aspen project uses WCF RIA Services to do the communication from the client to the server, we decided to use WCF RIA Services because it generates a good “base” for our ViewModel. When writing unit-test when using WCF RIA Services, we “have” to mock the WebDomainClient which is an class passed to the client-side DomainContext constructor. The WebDomainClient will handle the communication with the DomainService. When we write our tests we don’t want to mock the WebDomainClient and we don’t want to have my ViewModel to be dependent on a DomainContext, so instead we use IViewModelDataChannel. An abstraction on top of WCF RIA Services. By using the IViewModelDataChannel we can remove WCF RIA Services and use other frameworks for the communication, for example WCF, WCF Data Services, Web Service etc. Here is an example of a IViewModelDataChannel created for getting the Members stored into Aspen:

public interface IListMemberViewModelDataChannel
{
   void GetAllMembersAsync(Action<IEnumerable<MemberPM>> getAllMembersCompletedCallBack);
}

 

The following is the implementation of the IViewModelDataChannel using WCF RIA Services DomainContext:

[Export(typeof(IListMemberViewModelDataChannel))]
public class ListMemberViewModelDataChannel : IListMemberViewModelDataChannel
{
   MemberDomainContext _memberDomainContext = new MemberDomainContext();

   public void GetAllMembersAsync(Action<IEnumerable<MemberPM>> getAllMembersCompletedCallBack)
   {
      _memberDomainContext.Load<MemberPM>(
               _memberDomainContext.GetMembersQuery(),
               loadOperation =>
              {
                   getAllMembersCompletedCallBack(loadOperation.Entities);
               }
              , null);
   }
}


Note: MEF is used to Export the IViewModelDataChannels and Import them into the ViewModel.

The following is a part of a ViewModel where the IListMemberViewModelDataChannel is used:

[ExportViewModel("ListMemberViewModel")]
public class ListMemberViewModel : BaseViewModel, IListMemberViewModel
{
        
 private IListMemberViewModelDataChannel _listMemberViewService;

 private IEnumerable<MemberPM> _members = null;
 private MemberPM _selectedMember = null;

 private bool _isBusy = false;

 [ImportingConstructor]
 public ListMemberViewModel(IListMemberViewModelDataChannel listMemberViewService)
 {
    Contract.Requires(listMemberViewService != null);
    Contract.Ensures(_listMemberViewService != null);

    this._listMemberViewService = listMemberViewService;

    this.IsBusy = true;
            
    this._listMemberViewService.GetAllMembersAsync(GetAllMembersCompleted);
  }

  private void GetAllMembersCompleted(IEnumerable<MemberPM> result)
  {
     this.Members = result;
     this.IsBusy = false;
  }

  public bool IsBusy
  {
     get { return _isBusy; }
     private set
     {
         _isBusy = value;
          NotifyPropertyChanged("IsBusy");
      }
  }

   public IEnumerable<MemberPM> Members
  {
      get { return _members; }
      private set
     {
         _members = value;
         NotifyPropertyChanged("Members");
      }
  }

  public MemberPM SelectedMember
  {
      get { return _selectedMember; }
      set
      {
          _selectedMember = value;
          NotifyPropertyChanged("SelectedMember");
      }
  }
}


By using MEF’s ImportConstructor attribute, we can inject parts into the constructor, in Aspen we are injecting the IViewModelDataChannel. The ViewModel will work against the IViewModelDataChennel instead of directly with WCF RIA Services. By doing so we can write unit-test against the ViewModel without needing to mock the WCF RIA Services WebDomainClient class, and we can also easily replace the way the ViewModel need to communicate with the server.


ViewModel Locator

A ViewModel locator is used to locate the ViewModel for a View and use MEF to compose all parts needed for the ViewModel, like importing a IViewModelDataChannel. We are using the ViewModel locator that John Papa wrote, with some modification because I thought Silverlight 4 RTM will have a bugged fixed, but that wasn’t true, so we will change the code back to John’s workaround later. So my suggestion is that you take a look at John’s code instead of the one added to Aspen at the moment.

 

If you want to know when I publish a new blog post, you can follow me on twitter: http://www.twitter.com/fredrikn

No Comments