MyBookmark/Modules.User.Application/EventOutbox/EventOutboxService.cs

34 lines
1001 B
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace Modules.User.Application.EventOutbox;
public interface IEventOutboxService
{
// Task EnqueueAsync(object domainEvent, CancellationToken ct = default);
Task EnqueueAsync(object payload, string type, CancellationToken ct = default);
}
public class EventOutboxService : IEventOutboxService
{
private readonly UserDbContext _context;
public EventOutboxService(UserDbContext context)
{
_context = context;
}
public async Task EnqueueAsync(object domainEvent, CancellationToken ct = default)
{
var message = new OutboxMessage
{
Id = Guid.NewGuid(),
OccurredOn = DateTime.UtcNow,
Type = domainEvent.GetType().AssemblyQualifiedName!,
Payload = JsonSerializer.Serialize(domainEvent)
};
_context.OutboxMessages.Add(message);
// Важно: SaveChanges НЕ вызываем — это делает UoW обработчика
await Task.CompletedTask;
}
}