Operation instead of query interceptor (WCF Data Services)

大憨熊 提交于 2019-12-12 09:42:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23203482/operation-instead-of-query-interceptor-wcf-data-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!