-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
116: Added database 'EventInterceptors'
- Loading branch information
Showing
9 changed files
with
54 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
API/ASSISTENTE.Persistence.Configuration/Interceptors/EventInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using ASSISTENTE.Domain.Common.Interfaces; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace ASSISTENTE.Persistence.Configuration.Interceptors; | ||
|
||
public class EventInterceptor(IPublisher publisher, ILogger<EventInterceptor> logger) : SaveChangesInterceptor | ||
{ | ||
public override async ValueTask<int> SavedChangesAsync( | ||
SaveChangesCompletedEventData eventData, | ||
int result, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var savedChangesResult = await base.SavedChangesAsync(eventData, result, cancellationToken); | ||
|
||
if (eventData.Context is null) | ||
return savedChangesResult; | ||
|
||
var domainEvents = eventData.Context.ChangeTracker | ||
.Entries<IEntity>() | ||
.Select(entry => entry.Entity) | ||
.SelectMany(entity => | ||
{ | ||
var events = entity.GetEvents(); | ||
entity.ClearEvents(); | ||
return events; | ||
}) | ||
.ToList(); | ||
|
||
foreach (var domainEvent in domainEvents) | ||
{ | ||
logger.LogInformation("Publishing domain event: {EventName}", domainEvent.GetType().Name); | ||
|
||
await publisher.Publish(domainEvent, cancellationToken); | ||
} | ||
|
||
return savedChangesResult; | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
API/ASSISTENTE.Persistence.MSSQL/ASSISTENTE.Persistence.MSSQL.csproj
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
API/ASSISTENTE.Persistence.POSTGRESQL/ASSISTENTE.Persistence.POSTGRESQL.csproj
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
API/ASSISTENTE.Persistence.POSTGRESQL/DependencyInjection.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters