TheDanielDoyle.EventDispatcher.EntityFrameworkCore 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package TheDanielDoyle.EventDispatcher.EntityFrameworkCore --version 2.0.2
NuGet\Install-Package TheDanielDoyle.EventDispatcher.EntityFrameworkCore -Version 2.0.2
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="TheDanielDoyle.EventDispatcher.EntityFrameworkCore" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TheDanielDoyle.EventDispatcher.EntityFrameworkCore --version 2.0.2
#r "nuget: TheDanielDoyle.EventDispatcher.EntityFrameworkCore, 2.0.2"
#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 TheDanielDoyle.EventDispatcher.EntityFrameworkCore as a Cake Addin
#addin nuget:?package=TheDanielDoyle.EventDispatcher.EntityFrameworkCore&version=2.0.2

// Install TheDanielDoyle.EventDispatcher.EntityFrameworkCore as a Cake Tool
#tool nuget:?package=TheDanielDoyle.EventDispatcher.EntityFrameworkCore&version=2.0.2

Entity Framework Core Domain and Integration event dispatch.

Quick start

  1. Construct a EntityFrameworkEventDispatcherContext class by passing in the DbContext itself, and an implementation of IEventDispatcher.

  2. Override the following SaveChanges() and SaveChangesAsync() methods with the example code below (or roll your own).

  3. Include the ClearEvents() and GetEventDispatcherContext() methods if required.

public override int SaveChanges(bool acceptAllChangesOnSuccess)
    {
        IEventDispatcherContext dispatcherContext = GetEventDispatcherContext();
        DispatchEvents<IDomainEvent>(dispatcherContext);
        int returnCode = base.SaveChanges(acceptAllChangesOnSuccess);
        DispatchEvents<IIntegrationEvent>(dispatcherContext);
        ClearEvents();
        return returnCode;
    }

public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
{
    IEventDispatcherContext dispatcherContext = GetEventDispatcherContext();
    await dispatcherContext.DispatchAsync<IDomainEvent>(dispatcherContext, cancellationToken).ConfigureAwait(false);
    int returnCode = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false);
    await dispatcherContext.DispatchAsync<IIntegrationEvent>(dispatcherContext, cancellationToken).ConfigureAwait(false);
    ClearEvents();
    return returnCode;
}

private void ClearEvents()
{
    foreach (EntityEntry<IEventObject> eventObject in this.ChangeTracker.Entries<IEventObject>())
    {
        eventObject.Entity.EventStore.ClearEvents();
    }
}
    
private IEventDispatcherContext GetEventDispatcherContext()
{
    return new EntityFrameworkEventDispatcherContext(this, this.GetService<IEventDispatcher>());
}
  1. Add the IEventObject interface to any of your DbContext entities.
    • This will require you to implement the IEventStore interface.
    • The following example will demonstrate.
public class User : IEntity
{
    public User()
    {
        EventStore = new EventStore();
    }

    public IEventStore EventStore { get; }
}
  1. To add a domain event, create a class which derives from IDomainEvent and you can add that to your eventstore on the entity.
    • Integration events should follow IIntegrationEvent.
    • You can create your own event types and use them as you wish. Dispatch them at your chosen point in the SaveChanges() or SaveChangesAsync() methods.
  • When save changes is called any event dispatch handlers registered will handle that event.

  • Your handlers should derive from the following interface to picked up by the IEventDispatcher:

    • IEventDispatchHandler<MyDomainEvent>
  • See the link to a sample project below, which should demonstrate clearly.

Samples

See https://github.com/TheDanielDoyle/EventDispatcher.EntityFrameworkCore/blob/develop/Samples/EventDispatcher.EntityFrameworkCore.Samples.InMemory/SampleDbContext.cs for an example DbContext implementation, which uses the EntityFrameworkEventDispatcherContext.

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 netcoreapp3.0 is compatible.  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
8.0.0 59 6/17/2024
7.0.0 300 11/28/2022
6.0.0 289 11/28/2022
5.0.0 290 11/28/2022
2.3.0 426 9/17/2020
2.0.2 437 12/7/2019