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.
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:
Telerik.Windows.Controls
Telerik.Windows.Controls.GridView
Telerik.Windows.Controls.Input
Telerik.Windows.Data
and to:
System.Windows.Controls.Ria
System.Windows.Data
* 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: