Entity Framework - Command Interception


Advertisements

In Entity Framework 6.0, there is another new feature known as Interceptor or Interception. The interception code is built around the concept of interception interfaces. For example, the IDbCommandInterceptor interface defines methods that are called before EF makes a call to ExecuteNonQuery, ExecuteScalar, ExecuteReader, and related methods.

  • Entity Framework can truly shine by using interception. Using this approach you can capture a lot more information transiently without having to untidy your code.

  • To implement this, you need to create your own custom interceptor and register it accordingly.

  • Once a class that implements IDbCommandInterceptor interface has been created it can be registered with Entity Framework using the DbInterception class.

  • IDbCommandInterceptor interface has six methods and you need to implement all these methods. Following are the basic implementation of these methods.

Let’s take a look at the following code in which IDbCommandInterceptor interface is implemented.

public class MyCommandInterceptor : IDbCommandInterceptor {

   public static void Log(string comm, string message) {
      Console.WriteLine("Intercepted: {0}, Command Text: {1} ", comm, message);
   }

   public void NonQueryExecuted(DbCommand command, 
      DbCommandInterceptionContext<int> interceptionContext) {
         Log("NonQueryExecuted: ", command.CommandText);
   }

   public void NonQueryExecuting(DbCommand command, 
      DbCommandInterceptionContext<int> interceptionContext) {
         Log("NonQueryExecuting: ", command.CommandText);
   }

   public void ReaderExecuted(DbCommand command, 
      DbCommandInterceptionContext<DbDataReader> interceptionContext) {
         Log("ReaderExecuted: ", command.CommandText);
   }

   public void ReaderExecuting(DbCommand command, 
      DbCommandInterceptionContext<DbDataReader> interceptionContext) {
         Log("ReaderExecuting: ", command.CommandText);
   }

   public void ScalarExecuted(DbCommand command, 
      DbCommandInterceptionContext<object> interceptionContext) {
         Log("ScalarExecuted: ", command.CommandText);
   }

   public void ScalarExecuting(DbCommand command, 
      DbCommandInterceptionContext<object> interceptionContext) {
         Log("ScalarExecuting: ", command.CommandText);
   }

}

Registering Interceptors

Once a class that implements one or more of the interception interfaces has been created it can be registered with EF using the DbInterception class as shown in the following code.

DbInterception.Add(new MyCommandInterceptor());

Interceptors can also be registered at the app-domain level using the DbConfiguration code-based configuration as shown in the following code.

public class MyDBConfiguration : DbConfiguration {

   public MyDBConfiguration() {
      DbInterception.Add(new MyCommandInterceptor());
   }
}

You can also configure interceptor config file using the code −

<entityFramework>
   <interceptors>
      <interceptor type = "EFInterceptDemo.MyCommandInterceptor, EFInterceptDemo"/>
   </interceptors>
</entityFramework>
Advertisements