Interceptions in ADO.NET Data Services
Query Interception in Data Services
The ADO.NET data services comes with a plug in mechanism which
is called interception. That mechanism enables us to intercept a
data service’s incoming query or operation and to plug in custom validation,
access policy logic or whatever we like into the pipeline of that data service.
For example, if I’ll like to validate that a course has more then three days, I
can implement an interceptor that will do that filter.
How to Implement Query Interception?
Query interception is enabled through the QueryInterceptor attribute.
We decorate the intercepting method with that attribute.
The QueryInterceptor gets an entity set name as parameter. We need to
return the expression that will be performed on every entity in the entity
set. An example for a query interception method signature can look like:
If I’ll like to implement a filter that returns only courses with duration bigger
then three days the method can look like:
How to Implement Modification Operation Interception?
Modification operation interception is enabled through ChangeInterceptor
attribute. We decorate the intercepting method with that attribute. As the
QueryInterceptor attribute, the ChangeInterceptor attribute gets as
parameter the entity set name as parameter. An example of method
signature of such a method can look like:
Unlike the query interception methods, the modification operation
interception doesn’t return an expression. We need to have two incoming
parameters – the entity that the operation will perform on and the operation
type which is represented by the UpdateOperations enum.
Pay attention, to have the entity from the entity set you registered in the
ChangeInterceptor or else the interception won’t work.
The next example I’m throwing an exception if somebody tries to insert or
update a course to have more then five points credit:
The Full Example Code
Lets sum up, in the post I showed how you can use the interception
mechanism that comes with the data services implementation. That
mechanism can help us to hook operations like validation or access policy
logic into the data services in a very simple manner.