Some Astoria tips I learnt today

First of all let me start by saying that if you are doing Silverlight, you must learn Astoria, or as they call it nowadays, ADO.NET Data Services..

In a matter of minutes you can expose data from a database in a restful service that can be consumed by Silverlight ๐Ÿ™‚

Now here are some things I learnt todayโ€ฆ

The first tip is How can I have the Service on 1 server and the Silverlight app on another without having Cross Domain issuesโ€ฆ

The answer to this is this url

Its important that you install the v1.5 CTP2 since by default this is not the version that comes with .NET 3.5 SP1 (Please note you also have to follow the rest of the stepsโ€ฆ )

Another useful tip I learnt today is how to Expand a Property of a Property inside an entity (common when you have many to many relations). Example you have a Venue table that is linked to Sports with a table called VenueSportMapping. You can use XPath like syntax to load the property value.

   1: var query = from x in context.Venue.Expand("VenueSportMapping/Sport")

   2:                         select x;

Thats all I have for todayโ€ฆ hope you find these 2 tips handy ๐Ÿ™‚

happy coding ๐Ÿ™‚

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