
Library
DDD reference app with Event Storming and hexagonal architecture
Replaces —

Who it's for
A senior developer building a new bounded context wants a concrete Java example showing how aggregates, domain events, and repositories fit together without becoming a Big Ball of Mud. This project provides working code and the ArchUnit rules needed to keep it clean.
You are working in a language other than Java.
A team facilitating their first Event Storming session needs a reference showing how the output, sticky notes and definitions, translates into bounded context packages and aggregate classes. The documented Big Picture and Design Level sessions provide a concrete before-and-after example.
A developer considering hexagonal (ports and adapters) architecture wants to see how it interacts with Spring Boot in practice, including how to wire dependencies without component scanning and how to enforce boundaries with ArchUnit.
You are already committed to a layered or modular architecture and are not evaluating alternatives.
A team that wants automated enforcement of their architecture boundaries can adopt the ArchUnit test patterns directly. The project shows rules that prevent model classes from depending on infrastructure packages and prevent Spring from leaking into the domain layer.
Your project does not use Java or ArchUnit.
The problem
Learning Domain-Driven Design from books leaves practitioners without a concrete, working codebase that shows how strategic analysis (Event Storming, bounded contexts) connects to tactical patterns (aggregates, domain events, repositories) and to real infrastructure choices (consistency model, persistence strategy, architecture enforcement). Most sample projects either skip the discovery process entirely or fail to show how architectural decisions are justified by domain complexity.
How it works
The project documents the full discovery process, Big Picture Event Storming, Example Mapping, and Design Level sessions, and shows how each sticky note became an aggregate, event, or policy in the codebase. The model-code gap is kept minimal so the ubiquitous language appears directly in class and method names.
The lending context uses ports-and-adapters to isolate the domain model from Spring and from the persistence layer. ArchUnit rules are written as unit tests that fail the build if model classes import infrastructure or Spring packages, preventing the architecture from eroding silently.
Both immediate consistency (via Spring application events) and eventual consistency (via a store-and-forward scheduled publisher) are implemented. The Groovy BDD test suite covers both options with the same scenario structure, using PollingConditions for the async path, so the choice can be revisited without rewriting tests.
Domain objects are immutable value types. Business operations return Vavr Either, Option, or Try monads instead of throwing exceptions or returning null. State transitions are encoded in the type system, for example placeOnHold accepts AvailableBook not Book, so invalid calls are rejected at compile time.
The project intentionally excludes JPA and Hibernate. Persistence is handled with JdbcTemplate and Spring Data JDBC, giving full control over SQL and removing lazy-loading and dirty-checking overhead. Both approaches are shown side by side so readers can compare the trade-offs.
Tests are written in Groovy with a domain-specific language that lets each test read as a plain-English scenario derived from the Example Mapping session, for instance: aCirculatingBook() with anyBookId() locatedIn anyBranch() placedOnHoldBy anyPatron().
Features
- Event Storming to code traceability
- Hexagonal architecture with ArchUnit enforcement
- Swappable consistency model
- Functional domain model with Vavr
- No ORM: Spring Data JDBC and plain SQL
- BDD test DSL
How to install
git clone https://github.com/ddd-by-examples/library.git cd library mvn spring-boot:run
Recommended for this setup
Strengths
- Complete discovery-to-code trail: The README links the Event Storming images, Example Mapping cards, and Design Level diagrams directly to the packages and classes they produced. Readers can trace a business rule from the sticky note wall to the ArchUnit test that enforces it.
- Honest treatment of trade-offs: The project does not advocate a single correct approach. It implements both consistency models, both persistence strategies, and a CRUD context alongside the full domain model, with inline commentary explaining when each choice is appropriate.
- Architecture protected by tests: Because ArchUnit rules are ordinary JUnit tests that run in CI, the hexagonal boundary cannot be crossed by accident. Teams can copy the pattern directly into their own projects to prevent architecture drift without relying on code review alone.
- Zero framework coupling in the domain model: The lending domain model carries no Spring annotations. Each bounded context has its own independent Spring application context, configured explicitly rather than through component scanning, which removes runtime coupling between contexts and mirrors the autonomy a microservice split would require.
Trade-offs
- Educational, not production-ready: The project is intentionally simplified for teaching. The domain covers only a subset of library operations, and several infrastructure concerns such as authentication, multi-tenancy, and production deployment are out of scope.
- Java and Spring-specific: All examples use Java 11, Maven, Spring Boot, Spring Data JDBC, and the Vavr library. Developers working in other languages or frameworks will need to translate the patterns manually rather than reuse the code.
- Steep learning curve for DDD newcomers: The project assumes familiarity with bounded contexts, aggregates, and Event Storming vocabulary. Readers who have not read foundational DDD material may find the architecture diagram and package structure difficult to follow without supplementary reading.
Tech stack
FAQ
It is a learning resource. The project models a public library domain to provide realistic business complexity, but it does not include authentication, a production UI, or operational features needed to run a real library. Its purpose is to demonstrate DDD patterns with working, testable code.
With Java 11 and Maven installed, run mvn spring-boot:run from the repository root. The application starts on port 8080. To run the full observability stack with Prometheus and Grafana, use docker-compose up and access Grafana at http://localhost:3000.
No. JPA and Hibernate are intentionally excluded. Persistence uses Spring Data JDBC and plain JdbcTemplate queries. The README explains that lazy loading, caching, and dirty checking are not needed given the small aggregate sizes, and that avoiding JPA reduces the object-relational impedance mismatch.
The project is designed so that each bounded context package can be promoted to a Maven module or a separate microservice without restructuring the domain model. Each context already has its own Spring application context and communicates through domain events rather than direct object references.
Both immediate and eventual consistency are implemented and tested. The immediate version uses Spring application events; the eventual version uses a store-and-forward publisher that polls every 3000ms. The README positions eventual consistency as the preferred direction but acknowledges that immediate consistency is a pragmatic starting point.