Inspired by Brad Abrams’ marvelous series of blog posts, I have decided to create a simple project demonstrating how to harness the enormous power of RIA Services with Telerik RadGridView for Silverlight. I have decided to use the new Chinook database after reading this wonderful post by Tim Heuer explaining how to work with relational data in the RIA Services paradigm. Make sure you have this database installed on the default instance of your SQL Server 2008 Express or you will have to modify the connection string as needed.
In one of my previous blog posts I have thoroughly explained How To Display Hierarchical Data with Row Details. Understanding Row Details is a must before you can go on.
So let’s get going.
1. Create a new Silverlight Application called MasterDetailsWithRIAServices. Host the Silverlight application in a new ASP.NET Web Application and enable .NET RIA Services.
2. In the ASP.NET Web Application create a new folder named Models.
3. Add a new “ADO.NET Entity Data Model” called “ChinookModel” to the Models folder. You can use all kinds of other models, but I decided to go with an Entity Framework model.
4. Select “Generate from database” and connect to the instance where you have installed the Chinook database. Save the entity connection settings as “ChinookEntities”.
5. Select the Album and Artist tables. The model namespace should read “Chinook Model’. The designer will then open and you should see something like this:
6. Rebuild the solution.
7. In the ASP.NET Web Application create a new folder named Services.
8. Add a new “Domain Service Class” and call it “Chinook Service”.
9. Choose the “ChinookEntities” DataContext. For this demo we won’t need editing and metadata classes:
10. Clicking OK will generate ChinookService.cs in the Services folder.
11. Entity Framework has some weird ways of naming the generated classes so I have renamed the query methods to be in plural form, i.e. GetArtists instead of GetArtist.
12. Since we are doing a master-details demo let’s add another query method that will return all albums given the id of the artist:
13. Rebuild the solution and let’s go to the client project.
14. Add references to the following Telerik assemblies:
* You can either download the “Latest Internal Build” version of RadControls for Silverlight from your Client.NET or use the assemblies from the source code archive I have provided.
15. In the client project add a new class called ArtistIdToAlbumCollectionConverter. This is where the magic will happen. This converter creates a new DomainDataSource with the query “GetAlbumsForArtistId” we have created earlier, passes in the artistId parameter and calls the Load method of the DomainDataSource. Finally it returns the DataView which populates the details grid asynchronously:
16. Now let’s use this converter. We will bind the ItemsSource of the details (albums) grid residing inside the RowDetailsTemplate to the ArtistId and let the converter do the rest of the job for us. Here is what the main page should look like at the end:
17. Build and run. Click on any of the row details toggle buttons.
Notice how the albums are retrieved asynchronously. Since you will be running this demo on your local machine, I have manually “slowed down” the connection so you can see what is really going on. For the master grid I have used Brad Abrams’ “hotel internet connection”© approach and for the details grid I have configured the DomainDataSource to pull data slowly.
Also, notice that when you click on a row that was already expanded, the data was cached and the row details appear immediately.
Now, let’s see what are my favorite albums, shall we:
You got to love RIA Services.
Here is the full source code of the demo. Enjoy!
Subscribe to be the first to get our expert-written articles and tutorials for developers!