Guides & Recipes — EricksonLopez.Events
Recipe 1: Reading Ambient Metadata via IEnvelopeEventHandler<TEvent>
Section titled “Recipe 1: Reading Ambient Metadata via IEnvelopeEventHandler<TEvent>”Use IEnvelopeEventHandler<TEvent> (services.AddEnvelopeEventHandler<TEvent, THandler>()) when handlers need EventEnvelope<TEvent>.Metadata (CorrelationId, CausationId, TenantId) without polluting the domain event record.
Recipe 2: Custom Pipeline Middleware (IEventMiddleware)
Section titled “Recipe 2: Custom Pipeline Middleware (IEventMiddleware)”Register services.AddEventMiddleware<TMiddleware>(lifetime). Note that in v2.0.0, singleton IEventMiddleware instances undergo an immutability check.
Recipe 3: Testing with EricksonLopez.Events.Testing
Section titled “Recipe 3: Testing with EricksonLopez.Events.Testing”using System;using System.Threading.Tasks;using EricksonLopez.Events.Contracts;using EricksonLopez.Events.Identifiers;using EricksonLopez.Events.Testing;using Xunit;
namespace Example.EventsTests;
public sealed record AccountCreated(Guid AccountId) : IDomainEvent{ public EventId Id { get; } = EventId.New(); public DateTimeOffset OccurredAt { get; } = DateTimeOffset.UtcNow;}
public sealed class AccountEventTests{ [Fact] public async Task PublishAsync_WhenEventIsValid_CapturesPublishedEvent() { var publisher = new FakeEventPublisher(); var evt = new AccountCreated(Guid.NewGuid());
await publisher.PublishAsync(evt);
Assert.Single(publisher.PublishedEvents); }}