event-handler

Implement idempotent, sequence-checked .NET query event handlers with MediatR and IUnitOfWork.

4|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill event-handler
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: event-handler
Source: https://github.com/FaysilAlshareef/dotnet-ai-kit/tree/main/skills/microservice/query/event-handler
Command: npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill event-handler

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Query-side projections in event-driven .NET systems must process events in order and tolerate duplicates; without clear patterns handlers can produce inconsistent read models, lost updates, or unnecessary retries.

Core Features & Use Cases

  • Idempotent creations: creation handlers detect existing entities and return success to avoid duplicate processing.
  • Strict sequence validation: update handlers enforce sequence == event.Sequence - 1 to detect gaps and out-of-order delivery and return appropriate completion or retry signals.
  • Unit of Work and named repositories: encourages IUnitOfWork with named repository properties and cancellation-aware persistence to ensure consistent commits.
  • Message-level return semantics: handlers return boolean to indicate completion (true) or retry/abandon (false), suitable for Service Bus or queue-based retry policies.
  • Use Case: building reliable query projections for Orders and Products that survive duplicates, replays, and eventual delivery ordering.

Quick Start

Implement an event handler that returns true for already processed or successfully applied events, returns false when prior events are missing, uses IUnitOfWork with named repositories, performs strict sequence checks before applying behavior, and calls SaveChangesAsync with the provided cancellation token.

Frequently Asked Questions about event-handler

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle idempotent event processing in .NET query projections?

Idempotent event processing in .NET query projections is handled by having creation handlers detect existing entities and return success, preventing duplicate processing while maintaining consistent read models. Update handlers enforce strict sequence validation to manage out-of-order delivery.

What is the best way to handle out-of-order or duplicate events in a MediatR query handler?

Handling out-of-order or duplicate events in a MediatR query handler requires strict sequence validation where update handlers enforce sequence == event.Sequence - 1. Handlers return boolean signals to indicate completion or request retries for missing prior events.

How do I implement strict sequence checking for distributed event handlers?

Strict sequence checking for distributed event handlers is implemented by validating sequence == event.Sequence - 1 before applying updates. This detects gaps and out-of-order delivery, returning a boolean false signal to trigger message retry when prior events are missing.

Does this event handler pattern work with IUnitOfWork and Service Bus retry policies?

This event handler pattern works with IUnitOfWork and Service Bus retry policies by using named repository properties and cancellation-aware SaveChangesAsync. Handlers return boolean signals directly indicating message completion or retry, fitting queue-based retry mechanisms.

Why do my query projections lose updates during event replays?

Query projections lose updates during event replays when handlers lack idempotency and sequence validation. Implementing strict sequence checks and detecting existing entities prevents inconsistent read models and unnecessary retries during duplicate or out-of-order event delivery.

When should I use boolean return signals in .NET event handlers?

Boolean return signals in .NET event handlers should be used to indicate message completion or retry for Service Bus and queue-based policies. Returning true signals successful processing or duplicate detection, while false indicates missing prior events requiring a retry.