如果可能的话,请你帮忙,是否有办法通过加载操作的通用方法
例如:
这是获取Departments Data并将其绑定到网格的常规方法
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); }
所以我的问题是有可能有这样的东西吗?
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); }
所以我很想知道是否有办法在一个方法中迭代上下文查询并将其与查询的名称进行比较,然后在匹配的查询上执行加载操作,如下所示
非常感谢你的帮助
最好的祝福,
解决方法
我已经做了类似的事情(通过一些调整可能会或可能不是你正在寻找的东西).我为我的类中创建了一个基类,它是一个客户端数据服务,它被注入到我的viewmodels中.它有类似下面的方法(默认值有一些重载):
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); }
然后我在dataservice中调用它:
Load<Request>(Context.GetRequestQuery(id),loaded => { // Callback },null);
编辑:
我不知道通过Ria Services可以做任何事情(但是其他人可能).你真正想要做的是在基类中有一个带有字符串的Load方法的重载,使用反射可以获得查询方法,例如(未经测试,我不知道这会产生什么影响):
// In the base class protected void Load(string queryName) { // Get the type of the domain context Type contextType = Context.GetType(); // Get the method @R_142_4045@ion 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");
… 或者其他的东西…
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。