Entity Framework - Interception de commandes

Dans Entity Framework 6.0, il existe une autre nouvelle fonctionnalité appelée Interceptorou Interception. Le code d'interception est construit autour du concept deinterception interfaces. Par exemple, l'interface IDbCommandInterceptor définit des méthodes qui sont appelées avant qu'EF n'appelle ExecuteNonQuery, ExecuteScalar, ExecuteReader et les méthodes associées.

  • Entity Framework peut vraiment briller en utilisant l'interception. En utilisant cette approche, vous pouvez capturer beaucoup plus d'informations de manière transitoire sans avoir à désorganiser votre code.

  • Pour implémenter cela, vous devez créer votre propre intercepteur personnalisé et l'enregistrer en conséquence.

  • Une fois qu'une classe qui implémente l'interface IDbCommandInterceptor a été créée, elle peut être inscrite avec Entity Framework à l'aide de la classe DbInterception.

  • L'interface IDbCommandInterceptor a six méthodes et vous devez implémenter toutes ces méthodes. Voici la mise en œuvre de base de ces méthodes.

Jetons un coup d'œil au code suivant dans lequel l'interface IDbCommandInterceptor est implémentée.

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);
   }

}

Enregistrement des intercepteurs

Une fois qu'une classe qui implémente une ou plusieurs des interfaces d'interception a été créée, elle peut être inscrite auprès d'EF à l'aide de la classe DbInterception, comme indiqué dans le code suivant.

DbInterception.Add(new MyCommandInterceptor());

Les intercepteurs peuvent également être enregistrés au niveau du domaine d'application à l'aide de la configuration basée sur le code DbConfiguration, comme indiqué dans le code suivant.

public class MyDBConfiguration : DbConfiguration {

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

Vous pouvez également configurer le fichier de configuration de l'intercepteur en utilisant le code -

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