Guides & Recipes — EricksonLopez.SharedKernel
Recipe 1: Deterministic Audit Timestamps (OperationTimeScope)
Section titled “Recipe 1: Deterministic Audit Timestamps (OperationTimeScope)”Introduced in v4.0.0, OperationTimeContext / OperationTimeScope establishes a consistent UTC timestamp across all entities mutated inside a unit of work.
Recipe 2: Testing Aggregates with DomainEventCollector
Section titled “Recipe 2: Testing Aggregates with DomainEventCollector”using System;using EricksonLopez.Events.Contracts;using EricksonLopez.SharedKernel;using Xunit;
namespace Example.SharedKernelTests;
public readonly record struct OrderId(Guid Value) : IEntityId<OrderId>{ public bool IsEmpty => Value == Guid.Empty; public static OrderId Create(Guid value) => new(value); public static OrderId Parse(string s, IFormatProvider? provider) => new(Guid.Parse(s, provider)); public static bool TryParse(string? s, IFormatProvider? provider, out OrderId result) { if (Guid.TryParse(s, provider, out var guid)) { result = new(guid); return true; }
result = default; return false; }}public sealed record OrderSubmitted(OrderId OrderId) : DomainEvent;
public sealed class OrderAggregate : AggregateRoot<OrderId>{ public OrderAggregate(OrderId id) : base(id) { } public void Submit() => RaiseDomainEvent(new OrderSubmitted(Id));}
public sealed class OrderAggregateTests{ [Fact] public void Submit_WhenCalled_RaisesOrderSubmittedDomainEvent() { var aggregate = new OrderAggregate(new OrderId(Guid.NewGuid())); aggregate.Submit();
var events = aggregate.DrainDomainEvents(); Assert.Single(events); Assert.IsType<OrderSubmitted>(events[0]); }}