Reputation: 41
Would you please help if it is possible, is there is a way to have generic way for load operation
For example: This is normal way to get Departments Data and bind it to grid
grid.ItemsSource = context.Departments;
LoadDepartmentsData();
private void LoadDepartmentsData()
{
EntityQuery<Department> query = context.GetDepartmentsQuery();
context.Load(query, LoadOperationIsCompleted, null);
}
private void LoadOperationIsCompleted(LoadOperation<Department> obj)
{
if (obj.HasError)
MessageBox.Show(obj.Error.Message);
}
So my question is it possible to have something like this?
grid.ItemsSource = context.Departments;
grid.ItemsSource = context.Departments;
LoadData(“GetDepartmentsQuery”);
private void LoadData(string queryName)
{
…..?
}
private void LoadOperationIsCompleted(LoadOperation obj)
{
if (obj.HasError)
MessageBox.Show(obj.Error.Message);
}
So I was wonder if there is a way to iterate the context queries in one method and compare it to the name of the query for example and then execute the load operation base on matched query as below
your help very appreciated
Best Regards,
Upvotes: 4
Views: 195
Reputation: 896
I've done something similar (that might or might not be quite what you're looking for with a bit of tweaking). I created a base class for what in my class is a client-side data service that gets injected into my viewmodels. It has something like the following method (with a few overloads for default values):
protected readonly IDictionary<Type, LoadOperation> pendingLoads =
new Dictionary<Type, LoadOperation>();
protected void Load<T>(EntityQuery<T> query,
LoadBehavior loadBehavior,
Action<LoadOperation<T>> callback, object state) where T : Entity
{
if (this.pendingLoads.ContainsKey(typeof(T)))
{
this.pendingLoads[typeof(T)].Cancel();
this.pendingLoads.Remove(typeof(T));
}
this.pendingLoads[typeof(T)] = this.Context.Load(query, loadBehavior,
lo =>
{
this.pendingLoads.Remove(typeof(T));
callback(lo);
}, state);
}
Then I call it in the dataservice:
Load<Request>(Context.GetRequestQuery(id), loaded =>
{
// Callback
}, null);
Edit: I don't know of any way to do that through Ria Services (someone else might, however). What you could do if you really wanted to was have an overload of the Load method in the base class that takes a string, which using reflection could get the query method e.g. (not tested, and I don't know what possible implications this would have):
// In the base class
protected void Load(string queryName)
{
// Get the type of the domain context
Type contextType = Context.GetType();
// Get the method information using the method info class
MethodInfo query = contextType.GetMethod(methodName);
// Invoke the Load method, passing the query we got through reflection
Load(query.Invoke(this, null));
}
// Then to call it
dataService.Load("GetUsersQuery");
... or something...
Upvotes: 1