Breeze filtering .Expand on server side

后端 未结 1 1093
栀梦
栀梦 2021-01-21 06:09

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 se

相关标签:
1条回答
  • 2021-01-21 06:14

    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;
     }
    

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

    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;
    }
    

    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

    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;
    }
    
    0 讨论(0)
提交回复
热议问题