DDD stands for Domain Driven Design/Development. I have done extensive research on DDD and put in a lot of effort to practice it in real business projects. in this article I share my insight of how to organize code with DDD taste.

1. Modules in Project

Before delving into the domain structure, it is really important to separate a project into various distinct modules instead of having everything in a jumbled mess. This is similar to vertical division rather than layer division. It’s like building a well-organized house where each room (module) has its specific purpose and is clearly bordering.

In my case, I use modules such as App, Business, External, and Antiseptic.

2. Layers in App Module

The App module represents the application framework. It is a technical module that contains all the specific framework codes and tasks. For instance, Spring Boot provides a web server, configuration system, controller system, and schedule system. Your application runs on this framework and requires some setup code.

I set up the framework code in the App module and separate it into several layers. These include:

I must note that controllers, TCP handlers, and cron jobs are input portals. They only perform codec and then delegate the actual job to business services.

3. Layers in External

The External module wraps code for calling other platform APIs and serves communication. For example, when integrating PayPal into a project, we can write the API calling code in this module.

I create layers and specific classes in this spot:

We use client classes in the Business module (actually used in Antiseptic). However, we serve to the external only in this specific spot.

Typically, I structure the layers in External like this. But if more complex server services are required, I still put domain layers and converter layers in it. I will explain these layers in the next module.

4. Layers in Business

Business is involved with the core business operations. Although it is often thought of as basic CRUD operations, in reality, it should encapsulate operation rules and processes rather than just being limited to CRUD.

A common operation process has the following steps:

I set up most of the prime domain layers in this module.

for example Money class is composed of a number and currency type, and has the rules to generate a number for local use.

an admin role might have a “AdminService” class that contains functions like “approveUserRegistration” to approve new user registrations; A user role, on the other hand, might have a “UserService” class with functions like “viewProfile”.

For example, when a new order is created from a user’s input (DTO object), the convert layer will convert it into a domain object that can be processed by the business layer. Similarly, when the result of a business operation (domain object) needs to be sent back as a response to the user, the convert layer will convert it back into a DTO object

5. Layers in Antiseptic

Antiseptic consists of classes that cover all calls to external services. However, it is designed to adapt to business domain operations and hides all the actual external client class APIs. like a shield that protects the internal workings of the application from the complexity of external services.