4

I was reading about query interceptors. I was disappointed because thats more like a filter instead of an interceptor. In other words you can eather include records or not include them. You are not able to modify records for instance.

If I want to create a query interceptor for my entity Users I could then do something like:

[QueryInterceptor("Users")] // apply to table users
public Expression<Func<User, bool>> UsersOnRead()
{
    return cust => cust.IsDeleted == false;
}

What if I instead create the operation: NOTE IS VERY IMPORTANT TO HAVE THE OPERATION NAME JUST LIKE THE ENTITY NAME OTHERWISE IT WILL NOT WORK

[WebGet]
public IEnumerable<User> Users()
{                    
    return this.CurrentDataSource.Users.Where(x=>x.IsDeleted==false);
}

Placing this method instead of the query interceptor makes my service behave exactly the same. Plus I have more power! Is taking this approach a better solution?

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • It's a very interesting take. Does the update to the entities work or does the WebGet override that as well? Also if you want to support the query options you will want to expose IQueryable instead of IEnumerable. I'm guessing the WCF Team didn't think of this situation so you might be on your own if you run into any issues. – CharlesNRice Apr 21 '14 at 20:49
  • That true if I want to update the reference I have to uncomment those operations. Once I add the service reference on the client application I can uncomment those operations and it works great. – Tono Nam Apr 23 '14 at 01:46

1 Answers1

5

I played around a little more with this and one of the issues is navigation properties won't be filtered. Let's say you have an entity called SalesPeople that has a link into IEnumberable of Customers

If you do

[QueryInterceptor("Customers")] // only show active customers
public Expression<Func<Customers, bool>> ActiveCustomers()
{
    return cust => cust.IsDeleted == false;
}

when you query your OData feed like WCFDataService.svc/SalesPeople?$expand=Customers the results set for Customers will still have the filter applied.

But this

[WebGet]
public IQueryable<Customers> Customers()
{                    
    return this.CurrentDataSource.Customers.Where(x=>x.IsDeleted==false);
}

When running OData query like WCFDataService.svc/Customers you will have the filtered list on active customers, but when running this WCFDataService.svc/SalesPeople?$expand=Customers the results set for the Customers will include deleted customers.

CharlesNRice
  • 3,219
  • 1
  • 16
  • 25