Monday, July 25, 2011

Creating a track and session hierarchy

In this exercise, we want the user to be able to view/edit tracks for the events. Since each event can have multiple tracks, this exercise will show us how to deal with master-detail relationships.

First, the tutorial says that our default applications, DomainService only returns a list of Events and not the Entities which are related to the Event. We could get it to do a lazy fetch, but they suggest that we replicate the GetEvents() method and refactor it so that it returns the Tracks and Talks also.

I created a new method in EventManagerDomainServices.cs, which will return the Tracks and Talks for the events. This method is for the EntityFramework. BY adding this method we are enhancing the Entity Framework's functionality.

public IQueryable GetEventsWithTracksAndTalks()
this.ObjectContext.Events.Include("EventTracks.Talks");
}

We will make our Event DataGrid bind to this Query.

&<riaControls:DomainDataSource
    AutoLoad="True"
 &;nbsp;  d:DesignData="{d:DesignInstance my:Event, CreateList=true}"
    Height="0"
    LoadedData="eventDomainDataSource_LoadedData"
    name="eventDomainDataSource"
    QueryName="GetEventsWithTracksAndTalks"
    Width="0" Margin="0,0,320,480">
  <riaControls:DomainDataSource.DomainContext>
    <my1:EventManagerDomainContext />
  </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

However, this is not enough. We have enhanced the Entity Framework, but we also need to tell RIA services to include this data. By default RIA services is very restrictive, and will not include the data.

[Include]
EntityCollection EventTracks { get; set; }


Adding the include annotation to the EventTracks property will ensure that the data comes from RIA Services.

Now we need to select Event.EventTracks from data sources, and drop it on the view next to the Event details grid. In Silverlight when we select a dependant property, it will automatically be bound to the selected master record.

What this means is that we will only see related Event Tracks, depending on the Event which is selected.

No comments:

Post a Comment