Skip to content

rdehuyss/EF.Interception

 
 

Repository files navigation

EF.Interception Build status

EF.Interception is small library that lets you intercept saving of entities in Entity Framework.

Intallation

Install the plugin from NuGet: Install-Package EF.Interception.

Usage

To enable interception, derive your database context from InterceptionDbContext instead of DbContext:

public class MyDbContext : InterceptionDbContext
{
    // -- SNIP --
}

You can then add interceptors by calling AddInterceptor. An interceptor must derive from Interceptor<T>:

public class AuditInterceptor : Interceptor<IAuditEntity>
{
    public override void PreInsert(IContext<IAuditEntity> context)
    {
        context.Entity.CreatedAt = DateTime.UtcNow;
        context.Entity.ModifiedAt = DateTime.UtcNow;
    }

    public override void PreUpdate(IContext<IAuditEntity> context)
    {
        context.Entity.ModifiedAt = DateTime.UtcNow;
    }
}

public class MyDbContext : InterceptionDbContext
{
    public MyDbContext()
    {
        AddInterceptor(new AuditInterceptor());
    }
}

Interceptor<T> has six methods you can override:

  • PreInsert
  • PreUpdate
  • PreDelete
  • PostInsert
  • PostUpdate
  • PostDelete

All methods takes an IContext<T> which has three properties:

  • Entity - The entity which is intercepted.
  • State - The entity's current state. This can be altered.
  • ValidationResult - The entity's validation result.

About

A library for intercepting EntityFramework actions, like insert, update and delete.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 87.0%
  • PowerShell 13.0%