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
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
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