Up Scoring goals with DevForce
DevForce 2010 Resource Center » Code samples » Additional code samples » Silverlight » Scoring goals with DevForce » Scoring goals: WinForm to Silverlight, converting the CRUD (Part 2 of 2)

Scoring goals: WinForm to Silverlight, converting the CRUD (Part 2 of 2)

Last modified on June 21, 2011 09:36

Well, it was not quite a straight plug and play as I hoped it would be but with some minor changes, I managed to “plug” the view in with the view model. There are many minor changes with the code but here are the ones worth noting:

  1. This piece of code below was the key. I had difficulty with keeping track of the changes to the collection, especially if an existing entity’s property has been modified. The CollectionChanged event does not have a flag that indicates a modified entity. So I digged into the BookShelf solution and found a technique that involves binding the DataForm CurrentItem with a current item property in the view model.
C#
  public Club CurrentClub;

   <toolkit:DataForm CurrentItem="{Binding CurrentClub, Mode=TwoWay}"/>
VB
 Public CurrentClub As Club
   Private ReadOnly Property CurrentItem () As <toolkit:DataForm CurrentItem="{Binding CurrentClub, Mode=TwoWay}"/>

2. This may not be obvious from the solution but DevForce entities already implement the IEditableObject interface. This means that I don’t have to do anything extra to undo my changes. DevForce implementation of the interface already takes care of that.

3. I had to modify each entity’s primary key to be an identity column in order to maintain it easier.

4. Modified my call to _mgr.SaveChangesAsync by passing in a delegate, _mgr.SaveChangesAsync(OnSaveCompleted) instead of subscribing to an event, op.Completed += Completed_Handler. I believe this provides a cleaner and better code testability.

5. And finally if you look at all the view’s xaml.cs files (MainPage.xaml.cs, ClubView.xaml.cs, PlayerView.xaml.cs and GameStatView.xaml.cs), you will see that there is absolutely no code here that reference the view model (MainPageViewModel.cs). It is all done in the xaml. This means that so far, I’ve successfully implemented a view that is independent of its view model.

6. One of my future code refactoring is to separate each entity’s view model into its own view model. Currently the MainPageViewModel class is handling the Club, Player and GameStat views.

Next up, validation!

Tags:
Created by DevForce on May 05, 2011 18:02

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade