user1891172
user1891172

Reputation: 31

OData & Entity framework adding extra where clauses to all queries

I have an OData endpoint hooked up to Entity Framework to expose DataServices but I'd like to shape the data that comes out based on some metadata at runtime to make the query more restrictive than the URL

For example:

http://services.odata.org/OData/OData.svc/Category(1)/Products?$top=2&$orderby=name

If this is a user who's located in Europe I'd like this to return products that have a region of 'europe' but I'd rather the url didn't have to have the filter supplied by the client like:

http://services.odata.org/OData/OData.svc/Category(1)/Products$top=2&$orderby=name&$filter=Region eq 'Europe'

I have found that Query Interceptors can be used for this sort of thing but it's a concept that will apply to all queries over a large number of entities so I was hoping there was a more general way of applying it to all entities rather than having to specify an Interceptor on every entity.

I'm also looking at hiding certain fields based on user rights so for example if a field is marked as sensitive I can either dynamically remove the field from the query or the results based on whether or not the user is allowed to view sensitive data. I think the technique I'm describing above would be a solution to both of these scenarios.

Modifying the url would probably be hit and miss so perhaps I can access the expression tree EF creates and add or remove items from it just before it's executed?

Just in case it's relevant I'm using the DataService base class to expose the data:

public class MyDataService : DataService

Which is quick and easy but might be making it hard to achieve what I want

Any help greatly appreciated - even if it's just a specific name for what I'm trying to achieve that will help with researching solutions

Upvotes: 3

Views: 731

Answers (1)

Matt Meehan
Matt Meehan

Reputation: 174

Unfortunately, there isn't an easy way to do this in WCF Data Services today (other than QueryInterceptors, as you mentioned). The team has heard requests for this sort of functionality quite often, and we are working on some improvements in this area, but can't commit to anything publicly just yet.

It is possible to use the custom provider interfaces (IDataServiceMetadata/QueryProvider) to completely customize most of what WCF DS does, but doing this over EF is a lot of work, and may not be possible to do perfectly. Again, the team is looking at ways to make customizing the EF provider easier (such as making our in-box implementations of those interfaces public), but again I can't commit to when that would be available.

ASP.NET Web API is another option for creating an OData service with a lot more flexibility, though you may need to write more code than with EF + DataService.

Upvotes: 2

Related Questions