TheDanielDoyle.EventDispatcher.EntityFrameworkCore
2.0.2
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
<PackageReference Include="TheDanielDoyle.EventDispatcher.EntityFrameworkCore" Version="2.0.2" />
paket add TheDanielDoyle.EventDispatcher.EntityFrameworkCore --version 2.0.2
#r "nuget: TheDanielDoyle.EventDispatcher.EntityFrameworkCore, 2.0.2"
// 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
Construct a EntityFrameworkEventDispatcherContext class by passing in the DbContext itself, and an implementation of IEventDispatcher.
Override the following SaveChanges() and SaveChangesAsync() methods with the example code below (or roll your own).
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>());
}
- 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; }
}
- 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 | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.0 is compatible. netcoreapp3.1 was computed. |
-
.NETCoreApp 3.0
- Microsoft.EntityFrameworkCore.Relational (>= 3.0.1)
- TheDanielDoyle.EventDispatcher (>= 2.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.