简体   繁体   English

使用WCF Ria服务加载的通用方法?

[英]Generic Way for Loading using WCF Ria Service?

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

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, 最好的祝福,

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. 我为我的类中创建了一个基类,它是一个客户端数据服务,它被注入到我的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: 然后我在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). 编辑:我不知道通过Ria Services(其他人可能)的任何方式。 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 eg (not tested, and I don't know what possible implications this would have): 如果你真的想做的话,你可以做的是在基类中有一个带有字符串的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");

... or something... ... 或者其他的东西...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM