Interceptions in ADO.NET Data Services

November 1, 2008

Interceptions in ADO.NET Data Services

In the following post ADO.NET Data Services Query Interception
I’m going to explain
the interception
mechanism of
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:

[QueryInterceptor("Courses")]

public Expression<Func<Course, bool>> FilterCourses()

{

   // implement interception logic

}

If I’ll like to implement a filter that returns only courses with duration bigger
then three days the method can look like:

[QueryInterceptor("Courses")]

public Expression<Func<Course, bool>> FilterCourses()

{

   return (c => c.Days > 3);

}

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:

[ChangeInterceptor("Courses")]

public void OnChangeCourses(Course course, UpdateOperations operation)

{

   // implement interception logic

}

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:

[ChangeInterceptor("Courses")]

public void OnChangeCourses(Course course, UpdateOperations operation)

{

   switch (operation)

   {               

      case UpdateOperations.Change:

      case UpdateOperations.Add:

         {

            if (course.Credit > 5)

            {

               throw new DataServiceException(

                  "Can’t have more then 5 points credit course");

            }

            break;

         }               

         default:

            {

               break;

            }

      }          

}

The Full Example Code

public class CoursesService : DataService<CoursesDataContext>

{

   // This method is called only once to initialize service-wide policies.

   public static void InitializeService(IDataServiceConfiguration config)

   {

      config.SetEntitySetAccessRule("*", EntitySetRights.All);

   }

 

   #region Interception Examples

 

   [QueryInterceptor("Courses")]

   public Expression<Func<Course, bool>> FilterCourses()

   {

      return (c => c.Days > 3);

   }

 

   [ChangeInterceptor("Courses")]

   public void OnChangeCourses(Course course, UpdateOperations operation)

   {

      switch (operation)

      {               

         case UpdateOperations.Change:

         case UpdateOperations.Add:

            {

               if (course.Credit > 5)

               {

                  throw new DataServiceException(

                     "Can’t have more then 5 points credit course");

               }

               break;

            }               

         default:

         {

            break;

         }

      }          

   }

 

   #endregion

}

Summary

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.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*