Implement CQRS pattern
✓Works with OpenClaudeYou are an architect implementing the CQRS (Command Query Responsibility Segregation) pattern. The user wants to set up a complete CQRS system with separate command and query handlers, event sourcing integration, and proper separation of concerns.
What to check first
- Verify your project has a message bus or event dispatcher (MediatR for .NET, simple EventEmitter for Node, or custom implementation)
- Check if you have a persistent event store or database to track state changes
- Confirm your read/write data models can be physically separate (different tables, schemas, or databases)
Steps
- Create a Command base class that defines an operation requiring side effects (e.g.,
CreateUserCommandwith user data) - Create a Query base class that defines a read-only request returning data (e.g.,
GetUserByIdQuerywith only an ID parameter) - Implement CommandHandler(s) that receive commands, validate input, emit domain events, and persist state
- Implement QueryHandler(s) that receive queries, read from a denormalized read model, and return data without modifying state
- Set up an EventStore or event log to track all domain events emitted by command handlers
- Create UpdatedProjection or ReadModel builders that subscribe to events and update the read-only view when events occur
- Configure your command bus/dispatcher to route commands to their handlers and your query bus to route queries to theirs
- Add an event publisher that ensures events from commands are persisted before handlers complete
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// Command and Query base classes
public abstract class Command { }
public abstract class Query<TResult> { }
public class CreateUserCommand : Command
{
public string Name { get; set; }
public string Email { get; set; }
}
public class GetUserByIdQuery : Query<UserReadModel>
{
public Guid UserId { get; set; }
}
// Domain event
public class UserCreatedEvent
{
public Guid UserId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime Timestamp { get; set; }
}
// Write model (command side)
public class CommandHandler
{
private readonly EventStore _eventStore;
private readonly EventPublisher _eventPublisher;
public CommandHandler(EventStore eventStore, EventPublisher eventPublisher)
{
_eventStore = eventStore;
_eventPublisher = eventPublisher;
}
public async Task Handle(CreateUserCommand command)
{
if (string.IsNullOrEmpty(command.Email)) throw new ArgumentException("Email required");
var @event = new UserCreatedEvent
{
UserId = Guid.NewGuid(),
Name = command.Name,
Email = command.Email,
Timestamp = DateTime.
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Architecture Skills
Other Claude Code skills in the same category — free to download.
Singleton Pattern
Implement Singleton pattern
Factory Pattern
Implement Factory pattern
Observer Pattern
Implement Observer/PubSub pattern
Strategy Pattern
Implement Strategy pattern
Repository Pattern
Implement Repository pattern for data access
Dependency Injection
Set up dependency injection
Event Sourcing
Implement Event Sourcing pattern
Want a Architecture skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.