Implements domain events queue processing and eventual consistency

This commit is contained in:
BinaryAlley committed 2025-01-04 17:42:40 +02:00
1 parent f96cfa4a2f
commit 80e2ce0f82
10 files changed
+198 -9

No files matched your search

@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Application.Common.Behaviors;
using Lumina.Application.Common.DomainEvents;
using Mediator;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.CodeAnalysis;
@@ -33,6 +34,8 @@ public static class ApplicationLayerServices
// register fluent validators
services.AddValidatorsFromAssembly(typeof(ApplicationLayerServices).Assembly);
services.AddScoped<IDomainEventsQueue, DomainEventsQueue>();
return services;
}
}
@@ -0,0 +1,34 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using System.Collections.Concurrent;
#endregion
namespace Lumina.Application.Common.DomainEvents;
/// <summary>
/// Represents a thread-safe queue for managing domain events.
/// </summary>
public class DomainEventsQueue : IDomainEventsQueue
{
private readonly ConcurrentQueue<IDomainEvent> _queue = new();
/// <summary>
/// Adds a domain event to the queue.
/// </summary>
/// <param name="domainEvent">The domain event to enqueue.</param>
public void Enqueue(IDomainEvent domainEvent)
{
_queue.Enqueue(domainEvent);
}
/// <summary>
/// Attempts to remove and return the domain event at the beginning of the queue.
/// </summary>
/// <param name="domainEvent">When this method returns, contains the domain event removed from the queue, or the default value of <see cref="IDomainEvent"/> if the queue is empty.
/// </param>
/// <returns><see langword="true"/> if a domain event was successfully dequeued, <see langword="false"/> otherwise.</returns>
public bool TryDequeue(out IDomainEvent domainEvent)
{
return _queue.TryDequeue(out domainEvent!);
}
}
@@ -0,0 +1,25 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
#endregion
namespace Lumina.Application.Common.DomainEvents;
/// <summary>
/// Interface for a thread-safe queue for managing domain events.
/// </summary>
public interface IDomainEventsQueue
{
/// <summary>
/// Adds a domain event to the queue.
/// </summary>
/// <param name="domainEvent">The domain event to enqueue.</param>
void Enqueue(IDomainEvent domainEvent);
/// <summary>
/// Attempts to remove and return the domain event at the beginning of the queue.
/// </summary>
/// <param name="domainEvent">When this method returns, contains the domain event removed from the queue, or the default value of <see cref="IDomainEvent"/> if the queue is empty.
/// </param>
/// <returns><see langword="true"/> if a domain event was successfully dequeued, <see langword="false"/> otherwise.</returns>
bool TryDequeue(out IDomainEvent domainEvent);
}