Black Friday Deals For Authors Are On Now!

Leanpub Header

Skip to main content

Domain-Driven Design in Modern C++

Domain-Driven Design in Modern C++
This book is 1% completeLast updated on 2026-11-29

Business rules in C++23, modeled on a clinic whose worst day looks a lot like your bug tracker. Sixteen chapters, every pattern as code you can build and test, and a clear line between what the compiler refuses and what only a boundary can catch.

Minimum price

$29.00

$39.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
About

About

About the Book

Plenty of C++ business code treats the domain as data to be shuffled. The rules end up in managers and validators, three call layers away from the field they protect, so a referral that spells `CARDIOLGY` reaches the database, two appointments land in room R4 at 10:30, and an override moves someone's follow-up with no trace of why.

Domain-Driven Design has an answer for each of those three. Almost all of its literature assumes a managed runtime, and I could not find the ideas worked out in modern C++. So I wrote this book.

What you get out of it

  • Fewer surprises after dark. A rule that lives in a type fails at build time, long before anyone stands outside room R4 waiting for a bed. A rule that needs live data fails at runtime, at a boundary you built to refuse it. Every chapter says which one it's handing you.
  • A vocabulary you can grep. The clinic's language becomes an `enum class` with twelve members, a name table with twelve rows, and a parser that refuses a thirteenth. A forgotten `case` stops the build, because `-Werror=switch` sits in every snapshot's own `CMakeLists.txt`. New developers on your team read the type and learn the domain.
  • Boundaries the build respects. Five bounded contexts, mirrored in namespaces and CMake targets, so code that reaches across a border fails on your machine. It doesn't wait for a reviewer to notice.
  • Failures that carry their reason. Business refusals come back as values through `std::expected`, chained with `and_then` and `or_else`, then translated once, at the edge, by an exhaustive switch that maps every domain failure to an HTTP status. The messages name the failure and not the patient, which is a privacy decision and not an oversight. Exceptions stay reserved for genuine faults.
  • Proof that an abstraction is free. Size, trivial copyability and compile-time evaluation get pinned by `static_assert`, and the loops that could drift get timed with the numbers printed. A wrapper that costs nothing becomes something you measured.
  • Judgment, and a way in. Chapter 15 scores twelve patterns against the eight failures and labels the ceremony as ceremony, so you can turn a pattern down with a reason instead of a preference. Chapter 16 then works a legacy codebase from the lowest-risk rung up, with a strangler seam at the booking boundary and no rewrite.

How the book is built

Sixteen chapters, one running example, five parts. Chapters 2 to 16 each ship a buildable snapshot with passing tests.

  • Part I, Orientation and strategy (chapters 1 to 2). The clinic, its people, its diary rules and its invented Tuesday, then the bounded contexts and the shared kernel that decide where a rule is even allowed to live.
  • Part II, The type system as a modeling tool (chapters 3 to 7). Closed vocabularies and exhaustive switches, a `TimeRange` that can't be inverted, rules that return instead of throw, identity that survives a rename, and the zero-cost contracts you prove rather than assume.
  • Part III, Behavior behind boundaries (chapters 8 to 11). An aggregate with one way in and no public setters, cross-aggregate policy values and overrides with recorded reasons, a repository port specified as a concept, and domain events dispatched after commit.
  • Part IV, Architecture in the compiler (chapters 12 to 14). Contexts turned into C++20 modules, read models materialized once, and a coroutine intake flow that compensates cleanly when a step fails.
  • Part V, Judgment (chapters 15 to 16). The audit, then the adoption path for code you inherited.

Front matter sets the conventions and the toolchain. The back matter carries setup steps, a cheat sheet, numbered further reading, the solutions index, and a glossary of ninety-eight terms.

The code

Fifteen chapter snapshots, one per chapter from 2 to 16, each a small CMake project with tests that pass. They build warnings-clean under `-Wall -Wextra -Wpedantic`. Demo output quoted in the chapters comes from real runs, and each diagnostic names the compiler that printed it, because wording travels badly between compilers. Where a claim needs a number, a number sits beside it.

Who it's for

C++ developers who want the business rules inside the model. Java and C# readers who've met the Blue Book and don't want to trade predictability for a managed runtime. Senior developers weighing C++23 for a domain-heavy system. You don't need a DDD background, since chapter 1 brings it, but you do need to be comfortable with C++ at a working level. This isn't beginner's book, and chapter 1 teaches a clinic rather than variables.

Ninety-seven exercises, sorted into recall, application and extension, each with a worked solution.

What isn't in it

Full event-sourced persistence, ORM-backed storage, distributed microservices. Tests and demos run against an in-memory repository, and the mock database adapter is a sketch that deliberately never compiles. I'd rather say so here than have you meet it in chapter 10.

C++ is a business language too. This book puts the rules where the compiler can help you keep them.

Author

About the Author

Eduard Ghergu, Ph.D.

For over 27 years, I’ve helped teams transform their codebases from legacy liabilities to scalable, maintainable systems through clean architecture, domain-driven design, and hands-on mentorship. My books and courses distill decades of real-world experience into actionable insights—no fluff, just proven patterns.

Contents

Table of Contents

Preface

  1. Why Domain-Driven Design?
  2. Who This Book Is For (and Not For)
  3. How to Use This Book
  4. Conventions
  5. Prerequisites and Setup
  6. About the Author

Chapter 1 — Meet Meridian Outpatient

  1. Domain-Driven Design in One Page
  2. The Building Blocks and Where They Belong
  3. Four Questions Before the Clinic Tour
  4. The Big Picture
  5. The Clinic the Diary Runs
  6. A Closed Cast
  7. The Diary Rules
  8. The Appointment Catalog
  9. Urgency and Who May Break the Rules
  10. A Tuesday Worth Remembering
  11. The Failure Log: F1-F8
  12. The Contract: How This Chapter Governs the Book
  13. Pitfalls
  14. Exercises
  15. Summary Checklist
  16. Bridge

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub