Consuming Ado.Net Data Services from WPF

I love playing around with new technologies so today I decided to start playing around with ADO.Net Data Services aka Astoria… The first time I saw this technology was a Tech Ed Barcelona… Mr. Pablo Castro himself was showing this beauty! I love this guy….. he is so COOL ๐Ÿ™‚

For those that never heard of Astoria I suggest that you have a look here. Long story short, Astoria gives us an easy way how to pull data out of a data model… So basically you can create a service with Astoria that exposes data in minutes ๐Ÿ™‚

What I will show today here, is how you can query data from an Astoria service from WPF….

So first of all we need a service. Right? ๐Ÿ™‚ I will not go into much detail on how to create such a service but you can have a look here for more information. I followed this example step by step and within minutes I had a service up and running ๐Ÿ™‚

Ok ok…. now lets have a look at how we can consume data from this service…

The first thing you will need to do is to create an object model. For example if you want to query data from the Products table you need to create a Product class. You can do this in 2 different ways.
– You can use the Webdatagen.exe (more info found here)
– Create a class manually

I choose to create the classes manually because Webdatagen.exe can give you problems when you have a relational Linq to SQL…. If you do not have any relations then go ahead and use this tool to generate your classes… (Note: You MUST set a namespace for your LINQ to SQL in order for the Webdatagen.exe to work more info here)

First thing you need to do in order to use the Astoria service in WPF, is to add a reference to the Microsoft.Data.WebClient.dll which can be found in the /Program Files/Reference Assemblies/Microsoft/Framework/ASP.NET 3.5 Extensions.

Now it’s time for us to create our mapping classes. The mapping classes are very simple to create. You just have to create a class with properties that have the same name as the properties in the classes generated by Linq To Sql. It is important that the properties that you create have matching names as the properties in the LinqToSql Data model you created otherwise they will be ignored. It is also important to add the Microsoft.Data.WebClient.Key attribute on the property that is the key of your table.
You can also choose to not create all the properties for a specific entity. You can even specify that all properties that are not mapped should go in a Dictionary. In order to do this you can use the ObjectBag attribute and decorate the class you created with it in order to specify which property (of type dictionary) can hold the values of the properties that you choose to exclude from the class…. your class should look something like this now

astoria class

Brilliant! So we are one step closer…. now that we have a class that can hold the data that we are going to get from the Astoria service we need to actually query this service ๐Ÿ™‚

This can be done really easily by using the WebDataContext class. Basically all you have to do is to create an instance of this class and call the CreateQuery<T> method. This method will return a WebDataQuery<T> which happens to implement the IQuerable<T> interface….. Yes! that is right! you can use LINQ to query the service ๐Ÿ™‚ so your code would look something like this

astoria query

Is this cool or what ??!!??

Now want to see how you can use all this with WPF ??? download the sample application and have a look yourself…. Please read the read me file supplied in the download for details on how to setup the demo app on your machine

Download Sample Application

10 thoughts on “Consuming Ado.Net Data Services from WPF

  1. hmm…but aren’t the underlying objects are now exposed directly via WCF? I dont see this as a much difference than accessing directly via providers??…

  2. yea …. that’s the whole point… you consume this as a ADO.Net Data service that’s what it is after all….

    the demo is just with WPF that’s all the difference ๐Ÿ™‚

  3. Pingback: Border Crossing Stats » Consuming Ado.Net Data Services from WPF C# Disciples

  4. Pingback: Scott Hanselman's Computer Zen - Real World Apps in Days not Weeks

  5. Hey,
    something is wrong with the zip file – 404 file not found… Could u give us another link?! Iยดm curious about the sample app! ๐Ÿ™‚

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s