refactor
This commit is contained in:
parent
e282e2ece3
commit
84457413b2
20 changed files with 224 additions and 246 deletions
|
@ -1,6 +1,10 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Common.Infrastructure.Outbox;
|
||||
using Femto.Modules.Auth.Models;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Femto.Modules.Auth.Data;
|
||||
|
||||
|
@ -17,4 +21,43 @@ internal class AuthContext(DbContextOptions<AuthContext> options) : DbContext(op
|
|||
builder.HasDefaultSchema("authn");
|
||||
builder.ApplyConfigurationsFromAssembly(typeof(AuthContext).Assembly);
|
||||
}
|
||||
|
||||
public override int SaveChanges()
|
||||
{
|
||||
throw new InvalidOperationException("Use SaveChangesAsync instead");
|
||||
}
|
||||
|
||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await EmitDomainEvents(cancellationToken);
|
||||
|
||||
return await base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private async Task EmitDomainEvents(CancellationToken cancellationToken)
|
||||
{
|
||||
var logger = this.GetService<ILogger<AuthContext>>();
|
||||
var publisher = this.GetService<IPublisher>();
|
||||
var domainEvents = this
|
||||
.ChangeTracker.Entries<Entity>()
|
||||
.SelectMany(e =>
|
||||
{
|
||||
var events = e.Entity.DomainEvents;
|
||||
e.Entity.ClearDomainEvents();
|
||||
return events;
|
||||
})
|
||||
.ToList();
|
||||
|
||||
logger.LogTrace("loaded {Count} domain events", domainEvents.Count);
|
||||
|
||||
foreach (var domainEvent in domainEvents)
|
||||
{
|
||||
logger.LogTrace(
|
||||
"publishing {Type} domain event {Id}",
|
||||
domainEvent.GetType().Name,
|
||||
domainEvent.EventId
|
||||
);
|
||||
await publisher.Publish(domainEvent, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue