You want to know how to apply and implement Domain-Driven Design (and good software architecture in general)? You’re looking for a real-world example? Great, you’ve found it! The idea of the LeasingNinja is to have one domain that is small enough to be grasped easily and big enough to show the different DDD1 concepts in a real-world, end-to-end example.

The LeasingNinja shows what I (Henning Schwentner) think is a good architecture—but it also compares that architecture to other styles, so you can make an educated choice for your own system.

There are different incarnations (or rather in-code-ations ;-) of the LeasingNinja. The domain always stays the same, so the incarnations can be compared easily. They differ in two ways:

  • Architecture. Bounded contexts with a rich domain model, a functional domain model, and—as a counterexample—a big ball of mud with an anemic domain model. More styles, like CQRS and event sourcing, are planned.
  • Language and technology. The same domain model in many programming languages, each one idiomatic for its language and tech stack. So you can see what DDD looks like in your language.

The good incarnations are meant to serve as blueprints—for humans and AI agents alike. A developer can study them and use them as a starting point for a new project. An agent can use them as a reference for how DDD is done in a particular language. The counterexamples show “how not to do it,” so be careful not to use those as blueprints… All incarnations are freely available on GitHub under the MIT license.

Note: The LeasingNinja is a work in progress. The incarnations differ in how complete they are: the Java incarnation is the most complete, others are still at an early stage.

OK, Show Me the Code!

Not so fast, young padawan! The first D in DDD stands for domain, and one important rule is that we need at least some understanding of the domain before we start coding. (If you want to ignore this warning, you can jump straight to the incarnations here.)

So, What Is the Domain in Our Example?

Imagine Bob wants to reduce his carbon footprint. He would like to swap his dirty gas-powered car for a clean new electric car. Unfortunately, he doesn’t have a lot of money. But he goes to a car dealer anyway.

Salesperson: “No problem: we buy the car for you and rent it to you for a monthly rate (installment).” This is called leasing.

Bob: “Great, how much will it cost me per month?”

The salesperson calculates the monthly rate. Bob signs the contract and the salesperson passes the contract on to the risk department. Then a risk manager looks at Bob’s credit rating, decides that it is good enough, and votes “yes” on the contract. Only now does the contract become legally valid. The risk manager informs the salesperson, who hands the keys to Bob.

Phew, that’s a lot of text! Let’s model it in a more graphical way—a domain story.

In this coarse-grained domain story, we get an overview of what is happening in our domain. We can also see many words of the domain language:

  • Verbs like “to sign” and “to vote”
  • Nouns like “contract,” “monthly rate,” and “credit rating”

All of them will become part of our ubiquitous language.

Now that we have gained some understanding of the domain, let’s see if we can find some context boundaries! Looking at our domain story, we can see interaction between the customer and the salesperson on the left. And there is work that the risk manager does alone. Considering this, we find two bounded contexts:

The leasing domain with contexts

There is the “Sales” context and the “Risk Management” context. We extract them from the domain story to draw the context map:

Context map

Going Deeper into the Details

The next step is to drill deeper into the individual bounded contexts. Let’s start with “Sales”:

Sales story

In this finer-grained domain story, we can find elements for our domain model. As a rule of thumb, we take:

  • the work objects and make classes out of them
  • the activities and turn them into methods/commands

This leads us to the following domain model:

Sales classes

Finally, Let’s Write Some Code

Now we have a nicely designed software system made of object-oriented domain models in bounded contexts. The next step is to implement them. That can be (and has been) done in different languages:

Language Sources
Java https://github.com/leasingninja/leasingninja-java-boundedcontexts-domainmodel
Java (before 14) https://github.com/leasingninja/leasingninja-javabefore14-boundedcontexts-domainmodel
PHP https://github.com/leasingninja/leasingninja-php-boundedcontexts-domainmodel
Python https://github.com/leasingninja/leasingninja-python-boundedcontexts-domainmodel
Swift https://github.com/leasingninja/leasingninja-swift-boundedcontexts-domainmodel
C# https://github.com/leasingninja/leasingninja-csharp-boundedcontexts-domainmodel
Ruby https://github.com/leasingninja/leasingninja-ruby-boundedcontexts-domainmodel
Kotlin https://github.com/leasingninja/leasingninja-kotlin-boundedcontexts-domainmodel
TypeScript https://github.com/leasingninja/leasingninja-typescript-boundedcontexts-domainmodel
JavaScript https://github.com/leasingninja/leasingninja-javascript-boundedcontexts-domainmodel
Object Pascal https://github.com/leasingninja/leasingninja-objectpascal-boundedcontexts-domainmodel

All incarnations follow the same principles:

  • Same domain, same behavior. All incarnations implement the same domain model and are verified by equivalent tests, so you can compare them directly.
  • Idiomatic, not translated. An incarnation is not “Java written in another language.” It uses the idioms of its language, even where that means differences in naming or structure. The same holds for everything around the code: build tools, frameworks, test frameworks, and dependency management are the ones native to the particular tech stack.
  • Each repository stands on its own. It needs nothing from the other repositories.
  • Current technology. Language versions, frameworks, and tools are kept up to date, unless an incarnation is about an older version on purpose (like Java before 14).

Functional Variant

The incarnations above all model the domain in an object-oriented way. A functional language suggests a different tactical design: instead of one Contract whose state changes when it is signed, the states become separate types and the type system forbids the illegal ones. This variant is still at an early stage.

Language Architecture Sources
F# Strategic: Bounded Contexts, Tactical: Functional Domain Model https://github.com/leasingninja/leasingninja-fsharp-boundedcontexts-domainmodel

Simpler Architectural Styles

So far, we have looked at how to build a nice domain-driven architecture. In real life there’s not always the time or the knowledge to build that. To compare, let’s look at simpler (not to say uglier) styles:

Language Architecture Sources
Java Strategic: Big Ball of Mud, Tactical: Anemic Domain Model https://github.com/leasingninja/leasingninja-java-bigballofmud-anemicdomainmodel

What about Different Technologies?

All Incarnations

The following table shows all the ways in which the same domain is currently implemented:

Language Architecture Technology Status Sources
Java Strategic: Bounded Contexts, Tactical: Domain Model UI: Spring Web MVC, Persistence: Hibernate largely fleshed out https://github.com/leasingninja/leasingninja-java-boundedcontexts-domainmodel
Java Strategic: Bounded Contexts, Tactical: Domain Model Java 11 (without records) largely fleshed out https://github.com/leasingninja/leasingninja-javabefore14-boundedcontexts-domainmodel
Java Strategic: Big Ball of Mud, Tactical: Anemic Domain Model   early stage https://github.com/leasingninja/leasingninja-java-bigballofmud-anemicdomainmodel
PHP Strategic: Bounded Contexts, Tactical: Domain Model   in progress https://github.com/leasingninja/leasingninja-php-boundedcontexts-domainmodel
Python Strategic: Bounded Contexts, Tactical: Domain Model   in progress https://github.com/leasingninja/leasingninja-python-boundedcontexts-domainmodel
Swift Strategic: Bounded Contexts, Tactical: Domain Model   early stage https://github.com/leasingninja/leasingninja-swift-boundedcontexts-domainmodel
C# Strategic: Bounded Contexts, Tactical: Domain Model   in progress https://github.com/leasingninja/leasingninja-csharp-boundedcontexts-domainmodel
Kotlin Strategic: Bounded Contexts, Tactical: Domain Model   in progress https://github.com/leasingninja/leasingninja-kotlin-boundedcontexts-domainmodel
Ruby Strategic: Bounded Contexts, Tactical: Domain Model   in progress https://github.com/leasingninja/leasingninja-ruby-boundedcontexts-domainmodel
TypeScript Strategic: Bounded Contexts, Tactical: Domain Model   in progress https://github.com/leasingninja/leasingninja-typescript-boundedcontexts-domainmodel
JavaScript Strategic: Bounded Contexts, Tactical: Domain Model   early stage https://github.com/leasingninja/leasingninja-javascript-boundedcontexts-domainmodel
Object Pascal Strategic: Bounded Contexts, Tactical: Domain Model Free Pascal and Delphi in progress https://github.com/leasingninja/leasingninja-objectpascal-boundedcontexts-domainmodel
F# Strategic: Bounded Contexts, Tactical: Functional Domain Model   early stage https://github.com/leasingninja/leasingninja-fsharp-boundedcontexts-domainmodel
  1. I assume some familiarity with the DDD jargon. To get it, or for a refresher, have a look at Eric Evans’s DDD Reference. ↩