[Translated] Ria service and relational data

Source: Internet
Author: User
Tags silverlight

As we all have original sin, when you read some technical demonstrations, most examples demonstrate a single table solution. Do you still remember when the single table system was last developed? :-)

In the demonstration of the RIA service, most of them are examples of single tables. How can we retrieve relational (Master/Slave) data through Ria? I use vs2010, Silverlight 4, and the WCF Ria service preview to demonstrate the following example. I also used the Chinook (helicopter) Sample Database, which is one of my favorite simple relational data examples.

Create a project with the RIA Service

This is easy. You only need to create a new Silverlight project and make sure that "Enable. Net Ria service" is selected (yes, it is not called WCF in the dialog box ).ProgramYou can click a simple button to retrieve the artist and related albums. These are the initial XAML:

 1: <Grid X: Name = "layoutroot" background = "white"> 2: <Stackpanel width = "400"> 3: <Button content = "Get artist information" X: Name = "getartistbutton" Click = "getartistbutton_click"/> 4: <Stackpanel orientation = "horizontal"> 5: <Stackpanel X: Name = "artistscontext"> 6: <Stackpanel orientation = "horizontal"> 7: <Textblock text = "artists:"/> 8: <Textblock text = "{binding elementname = listofartists, Path = items. Count}"/> 9: </Stackpanel> 10: <ListBox X: Name = "listofartists" width = "200" Height = "300" displaymemberpath = "name" itemssource = "{binding}"/> 11: </Stackpanel> 12: <Stackpanel X: Name = "albumscontext"> 13: <Stackpanel orientation = "horizontal"> 14: <Textblock text = "albums:"/> 15: <Textblock text = "{binding elementname = listofalbums, Path = items. Count}"/> 16: </Stackpanel> 17: <ListBox X: Name = "listofalbums" displaymemberpath = "title" itemssource = "{binding}" width = "200" Height = "300"/> 18: </Stackpanel> 19: </Stackpanel> 20: </Stackpanel> 21: </GRID>

Now we need to create relevant models and called domain services on the server side. The model I created using the Entity Framework (. NET 4) looks like this:

Now I want to create a domain service class for the model (remember to compile the solution after creating the model so that you can find them in the toolbox ). When you create a domain service class, make sure that you select the multiple selection boxes for "generate associated classes for metadata". After clicking this box, some stub services will be created for us.

Use domain service functions

The getartists and getalbums functions are available. As an example, we can associate the click button with the default function to get the artist list:

 
1:Chinookcontext CTX = new chinookcontext ();2:3:Private void getartistbutton_click (Object sender, routedeventargs E)4:{5:Artistscontext. datacontext = CTX. artists;6:CTX. Load (CTX. getartistsquery ());7:}

However, when a user clicks an artist, we want to display the artist's album instead of others. Therefore, we need to modify the domain service and add the following functions:

1:Public iqueryable <album> getalbumsforartist (INT artistid)2:{3:Return this. objectcontext. albums. Where (A => A. artistid = artistid );4:}

Now we can use this function to display the album information when you click an Artist:

 
1:Private void listofartists_selectionchanged (Object sender, selectionchangedeventargs E)2:{3:ListBox theList = sender as ListBox;4:Artist A = theList. selecteditem as artist;5:CTX. albums. Clear ();6:Albumscontext. datacontext = CTX. albums;7:CTX. Load (CTX. getalbumsforartistquery (A. artistid ));8:}

Great.

However, it does not seem necessary to process the details of this particular dataset. After all, if we know that we are operating on a clear master-slave relationship view, why does it not only include subdata and initialization requests? (And our dataset is not very large ).

Modify metadata

Do you still remember the generated metadata class? Now let's go back. The definition of artist metadata includes the following:

 
1:Public entitycollection <album> albums;

Note that it has an album set attribute. Great. We only need to modify some XAML bindings and obtain the selected album attributes? No, not yet. If you do this, you will not be able to obtain any data. Why? Because we did not notify the RIA Service to perform the necessary additional queries to obtain data. Add [include] to the top of the album set:

 
1:[Include]2:Public entitycollection <album> albums;

This is exactly what we need. Now we can add a function to the domain service class to obtain data:

 
1:Public iqueryable <artist> getartistswithalbums ()2:{3:Return this. objectcontext. Artists. Include ("albums ");4:}

Now we need to do some cleanup. Now we want to change the button and clickCodeTo get the getartistswithalbums query and replace the previous one.

Delete unnecessary code and use binding

Now we can delete the selectionchanged event processor for the artist ListBox and add some binding commands to the XAML, which looks like this:

 1: <Grid X: Name = "layoutroot" background = "white"> 2: <Stackpanel width = "400"> 3: <Button content = "Get artist information" X: Name = "getartistbutton" Click = "getartistbutton_click"/> 4: <Stackpanel orientation = "horizontal"> 5: <Stackpanel X: Name = "artistscontext"> 6: <Stackpanel orientation = "horizontal">7: <Textblock text = "artists:"/> 8: <Textblock text = "{binding elementname = listofartists, Path = items. Count}"/> 9: </Stackpanel> 10: <ListBox X: Name = "listofartists" width = "200" Height = "300" displaymemberpath = "name" itemssource = "{binding}"/> 11: </Stackpanel> 12: <Stackpanel X: Name = "albumscontext" datacontext = "{binding elementname = listofartists, Path = selecteditem}"> 13: <Stackpanel orientation = "horizontal"> 14: <Textblock text = "albums:"/> 15: <Textblock text = "{binding elementname = listofalbums, Path = items. Count}"/> 16: </Stackpanel> 17: <ListBox X: Name = "listofalbums" displaymemberpath = "title" itemssource = "{binding albums}" width = "200" Height = "300"/> 18: </Stackpanel> 19: </Stackpanel> 20: </Stackpanel> 21: </GRID>

Note that the datacontent of the album ListBox is now associated with the selecteditem of the artist ListBox by binding. The ListBox of the subsequent album has a {binding albums} command. This is because our artist queries the currentIncludingFor associated album data, we only need to reference the attribute.

Use it with caution

Although this example shows how easy it is to obtain the included results in the domain service query results, be careful when using them. If you have a database of 1000 customers and want to query all orders, it is wise to use this type of database. It should be used selectively.

You can download the solution of the preceding sample code here. Remember to install a Chinook database-it is not included in the solution.

I hope this will help you.

Reprinted. Please keep the author's name and original website address. Click here to open the original translated text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.