English 中文(简体)
Entity Framework Tutorial

Entity Framework Resources

Selected Reading

Entity F - Command Interception
  • 时间:2024-11-05

Entity Framework - Command Interception


Previous Page Next Page  

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.

pubpc class MyCommandInterceptor : IDbCommandInterceptor {

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

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

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

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

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

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

   pubpc 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.

pubpc class MyDBConfiguration : DbConfiguration {

   pubpc 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