How to make Entity methods in RIA DomainContext work asynchronously
Intro:
I have a RIA service on Silverlight application that generates the code from the .Web app. On a server side, I am using EF4 and a DomainService based on a EF4 model.
First example:
If I extend the DomainService with my own methods implementing IEnumerable or IQueryable the RIA generates the appropriate methods on its DomainContext class. Something like this:
public partial class SymbolicDataServ开发者_如何转开发ice
{
public IQueryable<Chemical> GetWeightedChemicals(int min, int max)
{
// ... some EF query here
}
}
RIA generates the method, so I can do something like this on Silverlight side:
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
SymbolicDataContext db = new SymbolicDataContext();
var chemicals = db.Load(db.GetWeightedChemicalsQuery(10,24), onChemicalsLoaded, false);
}
and then I respond to the loading in a onChemicalsLoaded callback function.
Second example:
If I want a method that does not return IEnumerable or IQueryable, but is a void method, I mark the DomainService's method with [Invoke] attribute:
[Invoke]
public void FlushChemical(Chemical chemical)
{
// some code that does what it does (with EF)
}
Now I can do something like:
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
SymbolicDataContext db = new SymbolicDataContext();
var chemical = db.GetWeightedChemicals(10,24).FirstOrDefault();
db.FlushChemical(chemical);
}
Third Example:
If I do:
public void ShakeChemical(Chemical chem, int timeShaking)
{
// Shake the chemical until it drops
}
RIA will create an Entity method on client side that enables me to do this:
private void btnShake_Click(object sender, RoutedEventArgs e)
{
Chemical chem = (ListBox)sender.SelectedItem as Chemical;
chem.ShakeChemical(22);
db.SaveChanges();
}
Question:
My question here is how to make the last two examples work asynchronously like the LoadOperation? In the first example, I can use callback on Load method to respond to the operation completion, but I have no idea how to make the other two functions asynchronous and I don't want my UI to block during the calls.
EDIT:
I see now that the second example's method has an overload with Action argument so I do have a callback for the second example. However, the question remains for the third example.
I'm assuming that the third scenario is updating a Chemical object in some way?? If this is the case then just look into "Named Update" methods for RIA Services. Hope this helps
精彩评论