EFCoreHooks 0.1.0

dotnet add package EFCoreHooks --version 0.1.0
NuGet\Install-Package EFCoreHooks -Version 0.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFCoreHooks" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFCoreHooks --version 0.1.0
#r "nuget: EFCoreHooks, 0.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EFCoreHooks as a Cake Addin
#addin nuget:?package=EFCoreHooks&version=0.1.0

// Install EFCoreHooks as a Cake Tool
#tool nuget:?package=EFCoreHooks&version=0.1.0

EFCoreHooks

This project provides a simple, attribute-based hooking system for all projects using all versions of Microsoft's Entity Framework Core 2.1 and above.

Installation

Make sure you have Nuget installed, the run

PM> Install-Package EFCoreHooks

Usage

First you must edit your ConfigureServices(IServiceCollection) so the hooking system can have access to the Dependency Injection Container

using EFCoreHooks;

/* ... */

public void ConfigureServices(IServiceColleciton services) {
   /* ... */
   services.ConfigureDbHooks();
   /* ... */
}

In most cases the only other thing you need to do is to have any dbcontext that you want to have hooks extends from HookedDbContext or HookedIdentityDbContext. Both classes are designed to be drop-in replacements for DbContext and IdentityDbContext respectively.

In cases where you need a base class other than DbContext or IdentityDbContext, you can use the extension methods directly by implementing IHookedDbContext, and overriding the SaveChanges and SaveChagnesAsync methods to call the extension methods instead of the base class.

public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
    return this.HookedSaveChanges(acceptAllChangesOnSuccess);
}

public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
    CancellationToken cancellationToken = default(CancellationToken))
{
    return this.HookedSaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}

Implementing the IHookedDbContext interface is quite simple:

public class HookedDbContext : DbContext, IHookedDbContext
{
    public HookedDbContext(DbContextOptions options, HookManagerContainer hooks) : base(options)
    {
        Hooks = hooks;
        Hooks.InitializeForAll(this);
    }

    public HookManagerContainer Hooks { get; }

    public int SaveChangesBase(bool acceptAllChanges)
    {
        // Mirror the base classes saved changes so the extension methods can access them
        return base.SaveChanges(acceptAllChanges);
    }

    public Task<int> SaveChangesBaseAsync(bool acceptAllChanges,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // Mirror the base classes saved changes so the extension methods can access them
        return base.SaveChangesAsync(acceptAllChanges, cancellationToken);
    }

    public override int SaveChanges(bool acceptAllChangesOnSuccess)
    {
        // Run the extension method instead of the base class's method
        return this.HookedSaveChanges(acceptAllChangesOnSuccess);
    }

    public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        // Run the extension method instead of the base class's method
        return this.HookedSaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0 741 7/10/2019

Initial Release