Skip to content

Basic RIA Services And DataGrid With VS 2010 Tooling (Article 2 of 7)

Updated: at 02:12 PM


  Title Of Each Article Video Included With Each Post
Part 1 Introduction To RIA Services In Silverlight (This Article) 7 Minutes
Part 2 Basic RIA Services And DataGrid With  VS 2010 Tooling 14 Minutes
Part 3 Adding A DataGrid With Connect The Dots DataBinding in VS 2010 13 Minutes
Part 4 Adding a Navigation Page to a Silverlight Business Application Template 11 Minutes
Part 5 Adding Speakers Page Template With Converters In Visual Studio 2010 Beta2 11 Minutes
Part 6 Adding A Sessions Page That Includes a Query Parameter In Silverlight VS2010 Beta2 10 Minutes
Part 7 Basic Authentication and Authorization In RIA Services 5 Minutes

[media id=2]

 

In this article, we will  build from scratch, using Visual Studio 2010 Beta 2 a simple application that lets us view Silicon Valley Code Camp Attendees that have authorized us to share their data.  We will be using no special RIA Services Visual Studio 2010 design tools to do this.  We will:

First, we need to create a new Visual Studio Project.  We do that by using the File/New Project and chose “Silverlight Business Application”.

image

Create The Entity Framework

Then, we need to create an Entity Framework Repository that will be the target of our Domain Service.  We do that by adding to the web project (not the top, Silverlight project), we just created and “ADO.NET Entity Data Model”.  We’ll call it ModelSVCC for Model Silicon Valley Code Camp.

image

We use the option “Generate From Database” as follows:

image

Choose the appropriate connection (in our case svcodecamp on our local Sql Server 2008 database).

image

For simplicity, choose all tables and views as follows.

image

Do a Rebuild/All to make sure all the template code get’s generated and we are done with our first step.

Create the Domain Service

Next, create the Domain Service.  This is done by adding again to the web project the Domain Service as follows.

image

Name it DomainServiceSVCC.cs again for Silicon Valley Code Camp.

Next check all tables and views, make sure “Enable Client Access” is checked and also “Generate associated classes for meta data”.  Enable Client Access adds an attribute to the generated domain class which tell Visual Studio (on building) to generate the same domain classes on the client (Silverlight) side.  This means that you are writing both your client and server code at the same time!  Very convenient.

image image

Now, to the method generated in the Domain Service class add an OrderBy Linq command so that the method GetAttendeesWithShare has an implicit order and Skip and Take LINQ will work and the total number of records can be easily obtained (notice the yellow highlighing is the code we added, the rest was there before).

image

Finally, Rebuild/All again and we are done creating the domain service class for this example.

Wire up an Appropriate Get Method that Returns in IQueryable

Now that we have our Domain Service built on the Server side (and of course it get’s exposed on the client side with Visual Studio 2010 template generating magic), we can go to one of the pages built in the default template, add a Silverlight DataGrid and assign our Domain Context to that. Let’s add the list of attendees to our Home Page.  We do that by going to the View\Home.xaml file and simply add a DataGrid tag to the page (see below highlighted in yellow, we simply typed that in or could have dragged it from the toolbar if we are so included).

image

Then, we go to the code behind of this page and add the following code to the “OnNavigateTo” enent of the Home.xaml page.

 var context = new DomainServiceSVCC();
 dataGrid1.ItemsSource = context.AttendeesWithShares;
 context.Load(context.GetAttendeesWithSharesQuery());

image

Now, when we rebuild all and run, we get:

image

When we click on the Id’s column header, we get the data sorted in reverse Id order, even though we never code an event for that!

image

Observations

So, what have we done?  Well, we’ve done a lot. first, we’ve create a Domain Service that uses WCF on the server side.  We created code on the Silverlight client that called this domain service.  We got databinding right through to our output.  Be very clear that in our code we added to the OnNavigate Event (repeated here)

image

we made a clear step across the network boundary.  We created the DataService context and when we loaded it, all the async magic just happened for us.  No wsdl’s, proxies to code, keep updated, etc.  All that actually happens for us, but we don’t have to worry about it.  I think I’m in love.

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects