Thinking Async 1 – Asynchronous Data Access in Silverlight

I have created a simple example of how you can do asynchronous business objects in a Silverlight application using a WCF service reference. This example is very simple and is not as fully abstracted as it could be but I am simply focusing on the aspects of retrieving data asynchronously and putting them into your business objects for now.

Download this examples source.

The first key is in the factory method, let’s see an example.

public static ExampleCollection FetchAll()


    ExampleServiceSoapClient client = new ExampleServiceSoapClient();

    client.FetchAllCompleted += (sender, e) =>


        ExampleCollection collection = e.UserState as ExampleCollection;




    ExampleCollection examples = new ExampleCollection();


    return examples;



In the example factory above we are attaching an event handler to the FetchAllCompleted handler of our WCF service client. This would normally be abstracted out into a Data Access Provider layer but for the sake of simplicity we are doing it using an inline lambda expression.

Next we are creating a new, empty, collection then calling FetchAllAsync then passing back the example collection to the caller. At this point the collection will actually be empty and at some time in the future the asynchronous call will complete and begin adding objects to the collection. This is important because for data binding we need the object we wish to be bound to immediately but we don’t necessarily need its data.

In order to notify the caller that we have added objects to our collection asynchronously we will need our collection to implement INotifyCollectionChanged. For non-collection objects you will need to implement INotifyPropertyChanged. The easiest way to do this for collections is to simply inherit from ObservableCollection<T>.

Here is a simple example of how you might implement a your asynchronous business object collection

public class ExampleCollection : ObservableCollection<Example>


    private void Load(ExampleInfo[] exampleInfo)


        foreach (ExampleInfo info in exampleInfo)


            this.Add(new Example(info));




    public static ExampleCollection FetchAll() …



Here is an example of a non-collection business object

public class Example : INotifyPropertyChanged


    public Example(ExampleInfo exampleInfo)





    private Example() { }


    private int _id;

    private string _name;


    public int ID


        get { return _id; }

        private set


            _id = value;





    public string Name


        get { return _name; }



            _name = value;





    private void Load(ExampleInfo exampleInfo)


        ID = exampleInfo.ID;

        Name = exampleInfo.Name;



    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)


        if (PropertyChanged != null)

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));



    public static Example Fetch(int id)


        ExampleServiceSoapClient client = new ExampleServiceSoapClient();

        client.FetchCompleted += (sender, e) =>


            Example example = e.UserState as Example;




        Example ex = new Example();

        client.FetchAsync(id, ex);

        return ex;




Remember, you just have to think in async!

Shawn Hargreaves at the Twin Cities XNA User Group

This May 15th, the Twin Cities XNA User Group is pulling out all the stops to bring you a Shawn Hargreaves (Microsoft, XNA Team) Double Feature! Shawn is flying in from Redmond for one night and while we have him here, he’ll be answering YOUR questions and presenting on the following topics:

1.) Defeating the Network Game Gremlins and

2.) Understanding Shaders.

For more information and to register, please visit

Meetings are held at Magenic (4150 Olson Memorial Hwy, Suite 400, Golden Valley, MN 55422) on the Third Thursday of each month at 6pm.

Twin Cities Code Camp, April 2008 Wrapup

On Saturday I attended the Twin Cities Code Camp and had a great time. I presented on Visual Studio Extensibility and felt like it went pretty well. In addition to my talk I attended presentations about the ASP.NET MVC Framework, XNA and the new Parallel Framework (or PFX). I found all to be very interesting and educational.

There were lots of people at this code camp; it felt like it was completely full compared to the last couple of code camps. As usual the quality of the food and the swag was great and I managed to walk away with a 5 user SourceGear fortress license that I’ll have to try out sometime soon.
If you are interested I am making available for download the Slides and Source Code from my VSX presentation.
From what I hear there will be another Code Camp in October, be sure to check in on the RSS feed regularly so you can ensure be sure to sign up in time!

CSharp Optionally Dynamic Calls

I’ve been reading about how the C# team is considering putting the option to call dynamic code within C# directly and I have been looking at some of the syntax they have been proposing publicly. It also seems like there are a handful of people who are proposing their own ideas of how to express various features of C# they wish existed so I thought I’d propose one of my own.

Personally I find the idea of a dynamic method or a dynamic block a little clunky. So I would like to propose a different possible solution.

The whole idea is that in C# the compiler has to know the type of a variable so it can guarantee that the method you are trying to call exists. This is largely the purpose of interfaces. However, there are some circumstances where you might prefer to call a method dynamically. The dynamic method block syntax Charlier Calvert is proposing is very reminiscent of the “unsafe” synatx in C# however if you really wanted to make the language more dynamic in general I think you could simply have the option to call the memeber with something other than a ‘.’ to indicate that it should be a dynamic call. Something more like:

string example = obj:Example();

In this case I’m suggesting that merely replacing the ‘.’ with a ‘:’ will cause the application to call the Example method dynamically. I belive this would still conform to the explicit nature of C# while making dynamic calls quite flexible. If the colon was an undesierable character it would be simple enough to just use something else.

In this case you would not get much intellisense related to the parameters or return value of Example wich I believe would prevent it from being used except in circumstances where it was absolutely needed. Also you would only get runtime exceptions if the types of the parameters and return values were incorrect. It would probably have to assume that the return Type was actually an object if you used the “var” syntax.

Also, you would need some way to check to see if the member existed at the very least:

string example;
   example = obj:Example();

Of course, this should be essentially the same for fields, properties and events. For example:

string example = obj:Example; // field or property
obj:ExampleEvent += new EventHandler(OnExampleEvent);

Jeff Perrin on Obscuring HTTP

I was reading an article by Jeff Perrin about how ASP.NET attempts to abstract web development and came across this phrase:

Now the first problem with Webforms is not that it’s an abstraction, or even that it’s a leaky one (they all are). The problem is that what Webforms attempts to abstract away is actually simpler than the abstraction!

The second “problem” with Webforms is that not very many people know the first problem. I know I didn’t, until I saw how Rails, Monorail, and other frameworks are able to work with the underlying model of the Web, while still being terribly simple to understand and develop on top of. Making it easier to program for the Web is a laudable goal, I’m just not so sure that abstracting the technology that it’s built on top of to the point where it’s unrecognizable is the way to go about doing it.

To me this rings very true. I have been following the progress of the Microsoft MVC framework a lot lately and I can’t tell you how excited about it I am. It will be a giant relief to finally be rid of postbacks and viewstates!