Ask Claude to Implement an Event Sourcing Pattern
Event sourcing is one of those patterns that's conceptually clear but tricky to wire up correctly from scratch. Claude can design the whole structure for your domain.
claude "Implement a basic event sourcing setup in Laravel for a BankAccount aggregate. Include: a DomainEvent base class, AccountOpened/MoneyDeposited/MoneyWithdrawn events, an EventStore using a database table, and the BankAccount aggregate that rebuilds state by replaying events."
Claude will design cohesive, minimal classes — not a framework — keeping the core pattern clear:
class BankAccount
{
private float $balance = 0;
private array $recordedEvents = [];
public static function reconstitute(array $events): self
{
$account = new self();
foreach ($events as $event) {
$account->apply($event);
}
return $account;
}
private function apply(DomainEvent $event): void
{
match($event::class) {
MoneyDeposited::class => $this->balance += $event->amount,
MoneyWithdrawn::class => $this->balance -= $event->amount,
default => null,
};
}
}
Ask Claude to extend it with projections, snapshots, or a process manager once the core is solid. Build incrementally.
Event sourcing pays dividends in auditability — Claude helps you get the foundation right.
Log in to leave a comment.
The /security-review command scans your uncommitted changes for injection vectors, auth gaps, hardcoded secrets, and other common vulnerabilities.
The SessionStart hook fires when any session begins or resumes, making it ideal for loading environment variables and running one-time setup scripts.
Ask Claude to write property-based tests for your functions using fast-check — it identifies the mathematical invariants in your code and generates tests that cover inputs you'd never enumerate by hand.