Aggregate design   Aggregate design

Most business domains have very interconnected Entities, sometimes up to the point where there is always a path going from any Entity to any other. We can try to minimize the number of associations in our design, and this is a good practice but it can lead to a translation loss between business and software. In a typical object model, it is difficult to clearly see the boundaries of a change. This is particularly acute in systems with concurrent access such as Web applications.

Anemic Domain Model   Anemic Domain Model

An Anemic Domain Model is something that can be found a lot of projects and, astonishingly, is often viewed as a good practice. But as you may already have deduced from its name, it doesn’t sound like something desirable and certainly not in DDD-oriented software.

Entities and Value Objects   Entities and Value Objects

Modeling business concepts with objects may seem very intuitive at first sight but there are a lot of difficulties awaiting us in the details. To clarify the meaning of model elements and propose a set of design practices, Domain-Driven Design defines three patterns that express the model: Entities, Value Objects and Services. This chapter will focus on Entities and Value Objects, leaving Services for their own chapter.

JPA with the business framework   JPA with the business framework

While you can do plain JPA by injecting EntityManager anywhere, it is better to define an architectural layer where you encapsulate persistence-related operations. In the business framework, persistence is confined to Repositories. JPA repository Default JPA repository The JPA add-on will provide a default JPA repository implementation for every aggregate that does not have a custom one. Two cases may occur: You don’t have any custom repository interface an particular aggregate.

Aggregates

An aggregate is a cluster of cohesive entities and value objects that is treated as a single unit.
Aggregates have clear boundaries and are loosely coupled to each other.

Bibliography

To go beyond what is described in this documentation, we recommend a few books which will help you to better understand DDD, independently of the business framework. Domain-Driven Design community This is a major online resource about Domain-Driven Design with a lot of contents to discover and learn about it. Online community Eric Evans: Domain-Driven Design Reference This book by Eric Evans is a summary of patterns and definitions of Domain-Driven Design.

Business code

Now it is time to spice up our «Hello World» application with some business code. With SeedStack business framework, it is easy to write clean and maintainable business code. Domain-Driven Design Domain-Driven Design is a software approach built on the idea of solving business problems through code. This is done by focusing on the heart of the business logic, to distill a design that can solve business problems. The business framework helps with the implementation of that domain model.

Domain events

A domain event is used to represent something that happened in the domain.
It happened in the past and is of interest to the business.

Entities

An entity is used to represent a domain concept distinguished by an identity.
This identity must remain the same through the whole entity lifecycle.

Factories

A factory is responsible for creating a whole, internally consistent aggregate when it is too complicated to do it in a constructor.

Layers

Domain-Driven Design itself can be successfully applied in variety of software architectural styles. Some are very broad and tend to define every aspect of the system, others are more focused and try to address a specific demand. The business framework itself can adapt to several architectural styles. This documentation will focus on the traditional layered architecture from the Eric Evans DDD book, improved by the usage of the Dependency Inversion Principle.

Package layout

We recommend a well-defined package layout for organizing your business code according to the software architecture described above. The domain The root package for the domain layer is: [base.package].domain The domain can be composed of several sub-domains that can be specified as sub-packages: [base.package].domain.subdomain1 [base.package].domain.subdomain2 ... The domain package (or each sub-domain package if relevant) contains: A model package containing aggregates, each in its own package. An aggregate package contains The aggregate root along with other related entities and value objects, The factory interface if any, The repository interface if any, The aggregate policies if any.

Persistence

Now that we have a domain model, we would like to use some persistence with it. In Domain-Driven Design, persistence is done with Repositories which work on whole aggregates. We need some data first! To be able to test this, we need some sample data. A class implementing LifecycleListener will provide the opportunity to insert data at application startup. In the package infrastructure, create a SampleDataGenerator class: package org.generated.project.infrastructure; import javax.

Qualified injection

In the business framework, you always inject dependencies by interface not by implementation class. For each injection point, two situations are possible: If only one implementation exists for a specific interface, it is injected without ambiguity. If multiple exists for a specific interface, the injection point should be qualified to select the implementation to inject. This done by putting an qualifier annotation along the @Inject annotation. Built-in qualifiers SeedStack provides several built-in qualifiers.

Repositories

A repository is responsible for consistently storing and retrieving a whole aggregate.
It has a simple collection-like global interface and optionally domain-specific methods.

What is Domain-Driven Design ?

Domain-Driven Design is a software approach built on the idea of solving business problems through code. This is done by focusing on the heart of the business logic, to distill a design that can solve business problems. The business framework helps with the implementation of that domain model. It also provides default implementations and helpers to quickly address related needs like model mapping or pagination. What is DDD ? Domain-Driven Design (DDD) is a term coined by Eric Evans in his book «Domain-Driven design», published in 2003.