ADO.NET Data Services - propagate changes across tracked lists?
So here is the situation. I am using ADP.NET Data Services 1.5 CTP2 with Silverlight 3. My EF data model is (in short) like this:
CmsProfile
Id
Name
CmsEvent
Id
Title
CmsProfileEventLink
Id
ProfileId
EventId
So there is a many <-> many relationship between people and events. When I load the events within silverlight, I do it this way:
private void AsyncLoadEventsKickoff()
{
DataServiceQuery<CmsEvent> theQuery = dashboardService
.CmsEvents
.Expand("CmsProfileEventLinks")
.AddQueryOption("$orderby", "Title");
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
Dispatcher.BeginInvoke(
() =>
{
DataServiceQuery<CmsEvent> query =
asyncResult.AsyncState as DataServiceQuery<CmsEvent>;
if (query != null)
{
//create a tracked DataServiceCollection from the
//result of the asynchronous query.
events = DataServiceCollection
.CreateTracked<CmsEvent>(dashboardService,
query.EndExecute(asyncResult));
AsyncLoadTracker();
}
}
);
},
theQuery
);
}
You will notice that I cannot get Expand()
to actually drop the next level and get me the event link details. It will only really tell me if there ARE event link records or not.
I put all the events into a grid (SelectionGrid
), and when you click one, I want to load ANOTHER grid (EventsGrid
) with the people that are related to this event. I do this by loading the grid with CmsProfileEventLink
objects and then drilling down on the DataMemberPath
to the profile name. In theory, this allows the grid to add new links for me - when a row is added I give it an Id
, and set the CmsEvent
to the current event, take the user input for the Profile
and blammo - new linked record.
In a perfect world I could just set peopleGrid.ItemsSource = EventsGrid.Selecteditem.CmsPeopleEventLinks
and the whole thing would work as expected. However since the expansion doesn't got that deep, I can't.
As a workaround I have loaded all the CmsProfileEventLinks
the same way into a "links" variable. So when you select a event I do this (ugly, ugly, ugly) to show the profiles...
private void Sync_EventsGrid()
{
var item = SelectionGrid.SelectedItem as CmsEvent;
if (item.CmsEventProfileLinks != null)
{
DataServiceCollection<CmsEventProfileLink> x =
DataServiceCollection
.CreateTracked<CmsEventProfileLink>(
dashboardService,
links.Where(p => p.CmsEvent == item));
EventsGrid.ItemsSource = x;
}
}
The problem is... that if a change is made within EventsGrid
it does NOT propagate back to l开发者_如何学Pythoninks context, even though they both share a DataService
context. The net result? if you select a different event and come back, the EventsGrid
does NOT show the recently added record. If you refresh the application, forcing it to re-read links from the database? it picks it up.
So I need any of the following...
A way to do the 2 level expansion of the
CmsEvent
record on initial load so I can simply pass its links property to the second grid (preserving the context)A better way to get a filtered view of "links" that does NOT spawn an independant context that doesnt update
A way to notify the "links" object that it should refresh, preferably without forcing it to go all the way back to the server via a async call - since the data clearly has been updated in the local context.
Any hints?
You could loop over the expanded property and call LoadProperty
on each one to expand its children, like so:
DataServiceQuery<CmsEvent> query
= asyncResult.AsyncState as DataServiceQuery<CmsEvent>;
if (query != null)
{
//create a tracked DataServiceCollection from the
//result of the asynchronous query.
events = DataServiceCollection.CreateTracked<CmsEvent>(dashboardService,
query.EndExecute(asyncResult));
AsyncLoadTracker();
foreach(var link in events.SelectMany(e=> e.CmsProfileEventLink))
{
dashboardService.LoadProperty(link, "CmsPeopleEventLinks");
}
}
精彩评论