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


  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:

  • Create the Entity Framework Repository
  • Create the Domain Service
  • Wire up an Appropriate Get Method that Returns in IQueryable
  • Call From the Client Code the Domain Service
  • Show The Results
  • Observations

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.

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. excuse me
    My issue was resolved
    thanks

  2. hi Peter
    Thanks for your explanations.
    I have problem in
    protected override void OnNavigateTo (NavigationEventArgs e)
    {
    var context = new DomainServiceSVCC ();
    dataGrid1.ItemsSource = context.AttendeesWithShares;
    context.Load (context.GetAttendeesWithSharesQuery ());
    }

    Does not DomainServiceSVCC in MainPage
    define for me namespaces and place this proceduer

  3. You need to include a “using” statement in the code behind of the page, don’t you?

  4. Hi Peter,
    Your tutorial looks very nice.

    I follow each step of your tutorial, but i get no data in my datagrid.
    It doesn’t show any Error-Message.

    Could you say me, what could be the reason.
    Greets Lori

  5. Hi Chris,

    not that I know of, I think a domain service is probably the easy way to go. I have not done much of this since I did the tutorials so it’s not really fresh in my head.

  6. Hi, Thanks for the tutorial. I have a quick question that is somewhat related. I’ve built some code behind in an aspx.cs file, and have two arrays + some other floating variables that I would like to use in a silverlight charting tool. Is there a way to pass those data structures without creating a domain service?

  7. He Les,
    No database available. I still have not had a chance to clean this up. Also, I just tried downloading my solution and it looks like I must have built this with an pre-release of vs2010 because it does not compile with the RTM. Hopefully, I’ll get a chance, but I know in the next few weeks I’m swamped and will not get to it.
    -Peter

  8. Les Prigmore says:

    Is a copy of the database with demo data available? It would be nice to have to use with the application.

    Thanks………….Les

  9. Congratulations!
    Can you make videos for download zipped

Follow

Get every new post delivered to your Inbox

Join other followers: