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 1 of 2)

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

Last modified on May 05, 2011 18:05

Being new to Silverlight, I had a hard time deciding the structure of my code. This mainly has to do with how I will design my UI. And I’m not a UI person. 

In my WinForm days, I was used to code using event handlers and I was writing duplicate codes for each event, whether it be button clicks, selection changed, checkboxes, etc. That turned out to be a nightmare and made me somewhat reluctant in maintaining the code because of the complexities. I realized that I have to avoid doing the same thing for this project if I were not to stop half way.

Fortunately, there is a pattern called MVVM (Model View ViewModel) that solves exactly those issues. It allows me to separate my UI (View) concerns from my code structure concerns. Using this pattern, I no longer need to worry about writing event handlers or codes that depend on how I design my UI. Another great thing about this pattern is testability. It allows me to focus on testing early in the development phase without having to design the UI just so I can test each event and mouse click.

And fortunately again, I can refer to a DevForce app that already has these MVVM principles. The app is called BookShelf and is written by Ward Bell.

Obviously, the BookShelf solution contains more advanced MVVM principles and best practices than what I require at this stage of my development so I’m just going to pick and learn the patterns that I need and apply it as best as I understand it.

The attached DevForce Fantasy Soccer solution is still incomplete in terms of a fully working app. But then again, it will always be a work in progress. Here are the things that worth noting:

  1. No UI integration yet. I’m still figuring out how to design my UI.
  2. MainPageViewModel.cs – This is where the “events, clicks, loads, adds, deletes, etc” are being handled. This is the bridge between my database (Model) and my UI (View). As this app grows, some code here will be refactored and put in another “Services” assembly. A true MVVM pattern requires the ViewModel to not have any knowledge of the EntityManager and that will be one of the goals of my future code refactoring.
  3. ICommand – This is a really cool .NET feature where you can bind a button to a function. In other words, this replaces a button click event handler, and I can easily test my function without having to design a test button in the UI like I used to do.
  4. DevForceFantasySoccerUnitTest – This is where my testing is contained. I’m using a Silverlight Unit Test Application project and I followed these instructions.
  5. DevForce Coroutines – This helps me in writing multiple asynchronous calls within my tests that I can easily follow in a synchronous way. Without this, I would have struggled finding a way to write my tests as everything in Silverlight must be asynchronous.

My next step is to figure out how to design my UI and attempt to integrate it with my ViewModel. Here’s hoping that I’m on the right track.

Tags: Code Sample
Created by DevForce on March 22, 2011 19:15

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