skills/dotnet/patterns/domain-event/SKILL.md
Enable domain entities to publish events when significant business events occur, allowing decoupled side effects in Clean Architecture applications. Use when: - Domain entities need to notify other parts of the system about business events - Implementing event-driven architecture with decoupled handlers - Coordinating side effects after domain changes - Building reactive systems that respond to domain state changes - Implementing eventual consistency between bounded contexts Triggers: "domain event", "event publishing", "event handler", "domain notification", "event-driven", "side effect"
npx skillsauth add yeeehaooo/WorkSpace domain-eventInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Enable domain entities to publish events when significant business events occur, allowing decoupled side effects.
Applies to:
IDomainEvent interface defined in Domain layerIDomainEventDispatcher defined in Application layer, implemented in Infrastructure// Domain Layer
public interface IDomainEvent
{
DateTime OccurredOn { get; }
}
public class OrderCreatedEvent : IDomainEvent
{
public DateTime OccurredOn { get; init; } = DateTime.UtcNow;
public Guid OrderId { get; init; }
public Guid CustomerId { get; init; }
public decimal TotalAmount { get; init; }
}
public class Order : AggregateRoot
{
private readonly List<IDomainEvent> _domainEvents = new();
public IReadOnlyCollection<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();
public void Create(Guid customerId, decimal totalAmount)
{
// Business logic...
Id = Guid.NewGuid();
CustomerId = customerId;
TotalAmount = totalAmount;
// Raise domain event
_domainEvents.Add(new OrderCreatedEvent
{
OrderId = Id,
CustomerId = customerId,
TotalAmount = totalAmount
});
}
public void ClearDomainEvents()
{
_domainEvents.Clear();
}
}
// Application Layer
public interface IDomainEventDispatcher
{
Task DispatchAsync<T>(T domainEvent) where T : IDomainEvent;
Task DispatchAllAsync(IEnumerable<IDomainEvent> domainEvents);
}
// Handler
public interface IDomainEventHandler<T> where T : IDomainEvent
{
Task Handle(T domainEvent, CancellationToken cancellationToken = default);
}
public class SendOrderConfirmationHandler : IDomainEventHandler<OrderCreatedEvent>
{
private readonly IEmailService _emailService;
public async Task Handle(OrderCreatedEvent evt, CancellationToken ct)
{
await _emailService.SendOrderConfirmationAsync(evt.CustomerId, evt.OrderId, ct);
}
}
// Usage in Command Handler
public class CreateOrderHandler : ICommandHandler<CreateOrderCommand>
{
private readonly IOrderRepository _orderRepo;
private readonly IDomainEventDispatcher _eventDispatcher;
private readonly IUnitOfWork _uow;
public async Task<Result> Handle(CreateOrderCommand cmd, CancellationToken ct)
{
var order = new Order();
order.Create(cmd.CustomerId, cmd.TotalAmount);
await _orderRepo.AddAsync(order, ct);
// Dispatch events after commit
await _uow.CommitAsync(ct);
await _eventDispatcher.DispatchAllAsync(order.DomainEvents, ct);
order.ClearDomainEvents();
return Result.Success();
}
}
This pattern works well with:
development
Create reusable .NET atomic capability code snippets that can be directly copied and pasted. Use when: - Creating single-purpose code snippets - Building reusable code templates - Implementing atomic technical capabilities - Creating copy-pasteable code blocks - Building snippet library for common patterns Triggers: "create snippet", "code snippet", "reusable snippet", "atomic snippet", "copy-paste code"
development
Create Docker Compose configuration for containerized .NET application development and deployment. Use when: - Containerizing .NET applications - Setting up local development environment with dependencies - Creating multi-container setups (API + DB + Redis) - Defining service dependencies and networking - Building docker-compose.yml for development or production Triggers: "docker compose", "containerize", "multi-container", "docker-compose.yml", "docker setup"
tools
Create adapter structure for integrating third-party APIs in Clean Architecture applications. Use when: - Integrating external APIs or services - Creating HTTP client adapters for third-party services - Implementing API integration with error handling - Setting up adapter pattern for external dependencies - Building resilient external service integrations Triggers: "api adapter", "third-party api", "external service", "http client adapter", "api integration"
development
Enterprise backend structure built on Clean Architecture, DDD, CQRS, and Vertical Slice API Design with Dapper-first persistence. Use when: - Creating new enterprise backend projects - Implementing Clean Architecture with DDD and CQRS - Building vertical slice API endpoints - Using Dapper as primary persistence mechanism - Organizing modules by UseCase-driven and Model-driven separation Triggers: "dmis structure", "clean architecture", "enterprise backend", "DDD CQRS", "vertical slice", "dapper"