Wow – the gift that keeps giving.. I am *still* having fun updating my my simple Mix 09 Business Application demo. Would anyone be interested in a say 10 hour session at PDC2009 where we walk thought all of this stuff ;-). Anyway, in this section I want to take up a challenge that Ben Hayat sent me to show that Silverlight and RIA Services are really capable of building real business applications. That was too good bait for me to pass up. Apparently real business applications include more than one table, and even more than simple master details. But they also include hierarchal data. For example you have an Orders table that has an associated set of line items in a separate LineItems table, but the Orders keeps track of total sales, net, tax and number of lines, as each line gets added or deleted.

To fit this to my SuperHero Placement Service, let’s consider the business problem of keeping up with the very witty quotes SuperHero’s often make.. And further, let’s use some advanced AI-based scoring technology to rate each quote then have the SuperEmployee keep up with their total points count.

To do this, first let’s add a Quotes table

Then let’s add the associations

And setup the foreign key relationship.. One SuperEmployee can have Many quotes.

Then we can refresh our Entity Framework model as we saw earlier and this gets easier to see…

Now we need set up the Quotes entity to return to the client. First, we need to tell Entity Framework to include Quotes in queries to the Database for SuperEmployee…

public IQueryable<SuperEmployee> GetSuperEmployees()

{

returnthis.Context.SuperEmployeeSet

.Include("Quotes")

.Where(emp=>emp.Issues>10)

.OrderBy(emp=>emp.EmployeeID);

}

Now, we need to make sure the table gets sent to the client, so in the SuperEmployeeDomainService.metadata.cs file, let’s add the Quotes property and mark it as included. This ensures that we are sending the minimal information over the wire by default.

internalsealedclass SuperEmployeeMetadata

{

// Metadata classes are not meant to be instantiated.

private SuperEmployeeMetadata()

{

}

[ReadOnly(true)]

publicint EmployeeID;

[Include]

public Quotes Quotes;

Now, we need to add an Insert method for quotes.. Notice we don’t need Query, or Add because we don’t support those operations. If you wanted to update the SuperEmployee server entity you could do that here as well…

1:publicvoid InsertQuote(Quotes quote)

2: {

3:this.Context.AddToQuotes(quote);

4: }

Now, on the client,I wanted a very simple UI, so I replaced the DataForm with a ListBox to list all the quotes already attributed to this SuperEmployee and then a very simple TextBox for adding a new quote.

Note in line 10, we use the very advanced AI logic to determine the quality of each quote… I am told American Idol is moving to this model now that Paula Abdul is leaving ;-)

All the user needs to do now is hit Submit Changes and the the Insert method we wrote above is called for each item added.

Now, we’d like to show the total points for each employee.. in this scenario I only need the data for UI display, so I going to use a computed property on the client. We do that with a partial class of the SuperEmployee class, where we define the TotalCount property in the obvious way (lines 12-18). But we also need to raise a property changed notification when any new Quote is added. So we sign up for the property change notification when the Employee is loaded and rise this event (lines 3-10)..

1:publicpartialclass SuperEmployee

2: {

3:protectedoverridevoid OnLoaded(bool isInitialLoad)

4: {

5:base.OnLoaded(isInitialLoad);

6:this.Quotes.EntityAdded += (s, e) =>

7: {

8:this.RaisePropertyChanged("TotalPoints");

9: };

10: }

11:

12:publicint? TotalPoints

13: {

14: get

15: {

16:returnthis.Quotes.Sum(q => q.Points);

17: }

18: }

19: }

Then, we add a bit of xaml to the DataGrid and we get some our computed property..