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,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using Mediator;
using System;
#endregion
@@ -7,15 +8,15 @@ namespace Lumina.Domain.Common.Events;
/// <summary>
/// Interface defining a domain event.
/// </summary>
public interface IDomainEvent
public interface IDomainEvent : INotification
{
/// <summary>
/// Gets the id of the domain event.
/// Gets the Id of the domain event.
/// </summary>
Guid Id { get; }
/// <summary>
/// Gets the date and time when the domain event occurred.
/// </summary>
DateTime OccurredOn { get; }
}
DateTime OccurredOnUtc { get; }
}
@@ -0,0 +1,34 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Domain.Common.Exceptions;
/// <summary>
/// Custom exception for domain aggregates eventual consistency.
/// </summary>
public class EventualConsistencyException : Exception
{
/// <summary>
/// Gets the primary error associated with the eventual consistency failure.
/// </summary>
public Error EventualConsistencyError { get; }
/// <summary>
/// Gets a list of underlying errors that contributed to the eventual consistency failure.
/// </summary>
public List<Error> UnderlyingErrors { get; }
/// <summary>
/// Initializes a new instance of the <see cref="EventualConsistencyException"/> class.
/// </summary>
/// <param name="eventualConsistencyError">The primary error representing the eventual consistency issue.</param>
/// <param name="underlyingErrors">A list of additional errors contributing to the issue, or <see langword="null"/> if no additional errors exist.</param>
public EventualConsistencyException(Error eventualConsistencyError, List<Error>? underlyingErrors = null) : base(message: eventualConsistencyError.Description)
{
EventualConsistencyError = eventualConsistencyError;
UnderlyingErrors = underlyingErrors ?? [];
}
}
@@ -1,3 +1,8 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using System.Collections.Generic;
#endregion
namespace Lumina.Domain.Common.Models.Core;
/// <summary>
@@ -6,6 +11,8 @@ namespace Lumina.Domain.Common.Models.Core;
/// <typeparam name="TId">The type representing the unique identifier for the Aggregate Root. It should be a non-nullable type.</typeparam>
public abstract class AggregateRoot<TId> : Entity<TId> where TId : notnull
{
protected readonly List<IDomainEvent> _domainEvents = [];
/// <summary>
/// Initializes a new instance of the <see cref="AggregateRoot{TId}"/> class.
/// </summary>
@@ -22,4 +29,15 @@ public abstract class AggregateRoot<TId> : Entity<TId> where TId : notnull
{
}
/// <summary>
/// Retrieves and clears all domain events associated with this aggregate root.
/// </summary>
/// <returns>A list of all domain events that were raised by this aggregate root.</returns>
public List<IDomainEvent> PopDomainEvents()
{
List<IDomainEvent> domainEventsCopy = [.. _domainEvents];
_domainEvents.Clear();
return domainEventsCopy;
}
}