In this post I will talk about Operations & Interceptors functionalities included into ADO.NET Data Services.
For more information on Data Services, I suggest you to read my previous posts:
- How to request your ADO.Net Data Services;
- How to create an ADO.NET Data Services with Visual Studio 2010 beta2
If you need to aggregate an operation, instead of creating the rule on the client side (on each sort of client application), you can centralize it easily on the server side using Operators.
In the following example, GetProductsByColor returns all products of a specific color.
The WebGetAttibute is used to request your service via REST.
Inside the method, you just have to write your Linq query on the CurrentDataSource to get the desired result.
You also need to define the Service Operation Access Control. You have to grant read access to the operation. Here is the code to add in the InitializeService method:
InitializeService is the method where you already have granted access to the Data Service.
And finally, you can execute the code with this URL:
Interceptors is the Data Services mechanism to customize logic on the server side. With this mechanism, you can intercept queries and add specific restriction rules.
Example 1 : Interceptor on a query request
In the following example, you will create :
- a filter to check if the person requesting the DataService service on the Products entity is member of the Admin role (defined for example with ASP.NET role provider), else an exception is thrown;
- only Products where SellEndDate property is not null;
Open you DataService class. Don't forget to add at the top of you file:
Create the filter method on the product entity. You need to create a function prefixed with QueryInterceptor attribute. This attribute expects the name of the entity on which the interceptor will be applied (as defined in the Data Service Workspace http://localhost:34570/AdventureWorksService.svc/). The method has to follow a specific signature.
Example 2 : Interceptor on a change query (Add, Change, Delete, … operation)
In this example, I’m just checking that only Admins have granted privileges for adding,updating or deleting Product entity set. The signature of the method is a little bit different from previous example.
For more information, I suggest you to read MSDN documentation.