微风过滤.在服务器端扩展 [英] Breeze filtering .Expand on server side

查看:72
本文介绍了微风过滤.在服务器端扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试BreezeJS.有一个要求,我可以在客户端代码中使用.expand,但是基于用户的role,服务器端将不会返回请求的.expand类型的所有记录.我试图创建一个自定义的BreezeQueryable属性,并覆盖一个方法以首先尝试完全过滤掉多余的数据.但这是一个例外.

I'm trying out BreezeJS. There is a requirement that I can use .expand in the client side code, but based on the role of the user, the server side will not return all the records for the .expand requested type. I tried to create a custom BreezeQueryable attribute and override a method to completely filter out the extra data first just to try. But it threw an exception.

我在服务器端看不到任何可以执行此操作的入口点.

I don't see any entry point where I can do that on the server side.

请按照正确的方向引导我,或者如果无法实现,请告诉我.我只能访问通用IQueryable,如何对此执行查询?

Please guide me in the right direction, or let me know if that's not possible. I only have access to generic IQueryable, how do I perform queries on this?

以下是一些示例代码:

服务器:

[BreezeController]
[EnableCors("*", "*", "*")]
public class MyDataController : ApiController
{
    readonly EFContextProvider<MyDbContext> _contextProvider;

    public MyDataController()
    {
        _contextProvider = new EFContextProvider<MyDbContext>();
        _contextProvider.Context.Configuration.ProxyCreationEnabled = false;
        _contextProvider.Context.Configuration.LazyLoadingEnabled = false;
    }

    // GET api/<controller>
    //Trying to use a custom attribute to filter data here
    [CustomBreezeQueryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    [HttpGet]
    public IQueryable<MyData> GetAllData()
    {
        var data = _contextProvider.Context.MyData;
        return data;
    }
}

public class CustomBreezeQueryableAttribute : BreezeQueryableAttribute
{
    public override IQueryable ApplyQuery(IQueryable queryable, 
                       ODataQueryOptions queryOptions)
    {
        var data = base.ApplyQuery(queryable, queryOptions);
        //trying to filter out MyDataHistory for MyData for testing, 
        //it throws exception
        //data = data.OfType<MyDataHistory>(); 
        return data;
    }
}

客户端:

breeze.EntityQuery.from("GetAllData").expand('MyDataHistory')
                   .using(this.manager)
                   .execute()
                   .then((data) => {               
                        console.log(data.results[0]);
                        def.resolve(data.results);
                    });

这是使用OfType时获得的exception,我想过滤,无论如何不要使用它.

This is the exception I get when using OfType, and I would like to filter, not use that anyways.

{"DbOfTypeExpression requires an expression argument with a polymorphic result type that is compatible with the type argument."}

推荐答案

不太确定我了解您的问题,但是您可以通过EF包含"在服务器端执行扩展",如下所示:

Not entirely sure I understand your issue, but you can perform the 'expand' on the server side via an EF 'Include' like that shown below:

 [HttpGet]
 public IQueryable<Customer> CustomersAndOrders() {
   var custs = ContextProvider.Context.Customers.Include("Orders");
   return custs;
 }

将返回客户"对象,每个对象均已完全填充其订单"属性并加载到Breeze缓存中.

Which will return 'Customer' objects each with its 'Orders' property fully populated and loading into the Breeze cache.

如果要在服务器上实际禁止给定资源名称的扩展",则可以使用[BreezeQueryableAttribute].请注意,在下面的示例中,从支持的操作列表中省略了 AllowedQueryOptions.Expand .

If you want to actually suppress 'expansion' on the server for a given resource name you can use the the [BreezeQueryableAttribute]. Note that AllowedQueryOptions.Expand is omitted from the list of supported operations in the example below.

[HttpGet]
[BreezeQueryable(AllowedQueryOptions = AllowedQueryOptions.Filter | AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.OrderBy)]
public IQueryable<Employee> Employees() {
  return ContextProvider.Context.Employees;
}

[BreezeQueryableAttribute]支持与此处描述的Microsoft [QueryableAttribute]相同的参数:

The [BreezeQueryableAttribute] supports the same parameters as Microsoft's [QueryableAttribute] described here: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

如果您想实际限制/过滤扩展的内容,则只能通过自己执行过滤扩展来完成,这可能是通过"withParameters"传递给方法的参数来实现的(这是因为EF尚未实现)支持对包含"进行过滤.我尚未测试以下示例,但总体思路应该可行.

The other option if you want to actually restrict/filter what gets expanded can only be done by performing the filtering expansion yourself, possibly with the help of parameters passed into the method via 'withParameters' (This is because EF does not yet support filtering on 'Includes'. I have not tested the example below but the general idea should work.

[HttpGet]
public IQueryable<Employee> Employees(double minWeight) {
  var emps = ContextProvider.Context.Employees.Include("Orders").ToList();
  // remove selected orders from what gets returned to the client.
  emps.ForEach(emp => {
    var ordersToRemove = emp.Orders.Where(o => o.Freight < minWeight).ToList();
    ordersToRemove.ForEach(o => emp.Orders.Remove(o));
  });
  return emps;
}

这篇关于微风过滤.在服务器端扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆