Some of you likely noticed that my PDC09 demo included a stubbed out Search function that I didn’t really get to walkthrough during the talk. I thought I’d do a blog post showing how it is done.
To get started you need:
You can download the completed solution as well. and be sure to check out the full talk.
First let’s write a new method on the DomainService to return the search results. In this case I want to return any Plates who’s name matches (grouped by Restaurant) and any Restaurant who’s name matches.
In line 1, you see that we are defining a new query that returns Restaurants… this one has a different name, and takes a search term as an argument.
In line 4-6, we are getting all the restaurants that that have any plates that match the search term.
In line 7-14, we are looping through all those restaurants and manually adding the plates that match to the collection we will return. Effectively, we are manually creating the instance in just the shape we need it for the client.
In line 17-18, we are getting the Restaurants that match and that are not already included.
and finally, in line 21, we return the concatenation of the two queries.
Notice that this query is designed to return Restaurants AND the Plates that are related. By default, related entities are not included (in order to save bandwidth).. so we need to go into the metadata file and explicitly include them.
Now, in the Silverlight client. First we need to wire up the Search button on MainPage.xaml.. There we just need to follow the same pattern we saw in the earlier post:
Then the search page, again we build it the same way as we saw in the earlier post, by simply drag and dropping from the data sources window. Notice that we now have two different query methods for Restaurants. So we simply select the right one and then drag and drop on the form as we saw in the earlier post:
After we get the UI laid out correctly you end up with a pager and all the columns set right.
Then wire up the parameter to the SearchRestaurants query method to the query string…
Now i’d like to display the Plates for each Restaurant that is returned. To do that, i’ll make use of the RowDetails feature of DataGrid.
Run it and we get this sort of view…
Notice the URL includes the search term, so I can send this around in email to share my search results bookmark it for future reference.
Now, you might want to drill into the details on what of these plates… so let’s handle that “Details..” hyperlink. In the code behind for the search page, we handle navigating to the Plates page with a the right query string paramaters.
Now we need to make a slight tweak to the Plates page because it does no know about the plateId query string parameter.
The first few lines to handle the RestaurantID were already there, so we just needed to add the code to handle the PlateID.. Notice we don’t need to change the query method on the server for this, we just add a new where clause that will get sent to the server and executed there.
Again, notice the URL, something we can bookmark or send around in email, etc.
I hope you got something valuable from this walkthrough… You can download the completed solution as well and be sure to check out the full talk.