加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用WCF Ria服务加载的一般方法?

发布时间:2020-12-15 22:00:38 所属栏目:百科 来源:网络整理
导读:如果可能的话,请你帮忙,是否有办法通过加载操作的通用方法 例如: 这是获取Departments Data并将其绑定到网格的常规方法 grid.ItemsSource = context.Departments;LoadDepartmentsData();private void LoadDepartmentsData(){ EntityQueryDepartment query =
如果可能的话,请你帮忙,是否有办法通过加载操作的通用方法

例如:
这是获取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 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");

… 或者其他的东西…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读