Nov 17, 2026
DDD in Practice -- Social Relationships (Blog/Topic)
A DDD code walkthrough for blog and forum products: package decomposition, collaboration between the application and domain layers, and the publish-post and follow-user call flows.
1. Background
Earlier articles covered a lot of DDD theory: books, arguments, and summaries. Eventually some hands-on material is needed, and many readers have been asking for a DDD project codebase. As the saying goes, if you can do it, don’t just talk about it. But DDD does have a learning curve, and mastering it fully takes real digging.
So this article builds on the earlier social relationship modeling article and presents a DDD code sample for a blog and forum product.
2. Domain Analysis
2.1 Core Domains
- Blog post domain
- User membership domain
2.2 Supporting Domains
- Moderation domain
- Account authentication
- Entitlement domain
3. Building the Domain Services
3.1 Packaging Domain Services
To keep the demonstration simple, domain services are represented as packages. The domain analysis above gives us two core services: blog/topic, and member (or user). Two packages are enough:
com.coderman.domainservice
├── blogtopic
└── member
3.2 Subpackages of a Domain Service
XXXDO: data objectXXXBO: domain business objectXXXDTO: adapter object, data transfer object
For the package strategy, see COLA: https://blog.csdn.net/significantfrank/article/details/110934799
1. Domain Service Package
For demonstration purposes, every domain lives under this package, which acts as the top-level path:
com.coderman.domainservice
2. Blog/Post Domain Service Package
com.coderman.domainservice.blogtopic
2.1 Controller (Adapter) Package
Subpackages include the admin side (admin) and the web side (web):
com.coderman.domainservice.blogtopic.adapter
2.2 Application Layer Package
This package has more subpackages:
com.coderman.domainservice.blogtopic.app
Subpackages:
clientimpl: implementation of the APIs exposed to the outsidecommand: command modelsconsumer: handles external messagesexecutor: handles requests, including commands and queriesquery: queries
2.3 Client Package
Subpackages are api and dto. It simulates the service interfaces exposed by the Dubbo framework, implemented in the application layer:
com.coderman.domainservice.blogtopic.client
2.4 Domain Package
This package has more subpackages:
com.coderman.domainservice.blogtopic.domain
Subpackages:
ability: domain capabilitiesconvertor: object conversionfactory: object factoriesgataway: domain gateway interfacesmodel: domain models (subpackages: entity, event, valueobject)
2.5 Infrastructure Package
This package has more subpackages:
com.coderman.domainservice.blogtopic.infrast
Subpackages:
cache: caching – integrates a cache middlewareconfig: configuration – integrates a configuration middlewaredubbo: Dubbo framework package – the Dubbo adapterelasticsearch: search – integrates a search enginegatawayimpl: domain gateway implementationsmapper: persistence (subpackages: dao, dataobject)mq: messaging (subpackages: consumer, provider) – integrates a messaging middlewaremodel: domain models (subpackages: entity, event, valueobject)rest: integration with REST frameworks, similar to the Dubbo package
3. User Membership Domain Package
com.coderman.domainservice.member
Note: users, membership entitlements, and accounts are all placed in a single domain here. If you want to refine it further, they can be split into subdomains.
The difference: the package strategy is similar to the blog/post domain, but the business model packages under the domain package are separated:
com.coderman.domainservice.member.domain.model
Subpackages:
memberright: membership entitlement modelsuser: user information and account models
4. Call Flows
4.1 Publishing a Blog Post
- The client calls the controller method
publishBlog(). - The controller turns the request into a command through the command executor.
- The command executor validates the business logic and orchestrates the business calls.
- The application layer calls the domain gateway services (repository, factory).
- The domain layer executes the domain services.
- The domain layer calls the DAO to persist the blog data.
- The application layer calls the adapter to invoke the downstream blog content service.
- The infrastructure layer listens for messages.
- The infrastructure layer calls the domain service to consume the moderation result message.
- The domain layer calls the application layer’s consumer service to consume the message.
- The application layer sends the “post published” MQ message.
- The application layer calls the business notification service.
4.2 Following a User
- The client initiates following someone.
- The controller turns the request into a command through the command executor.
- The application layer calls the domain gateway services.
- The domain layer calls the DAO to persist the relationship data.
- The domain layer updates the statistics in the cache.
- The domain layer calls the business notification service.
5. Code Notes
5.1 Repository
Only the blogDemo project is available at the Git address below. Other business scenarios will be added later, so stay tuned.
https://gitee.com/codergit.com/dddin-action.git
5.2 Project Package Structure

5.3 Project Notes
- To keep the demonstration simple, mapper, cache, ES, and MQ are mocked with empty implementations. When you actually introduce the corresponding middleware, just replace the empty implementation in the matching subpackage.
- The project is a plain Spring Boot application, so you can try implementing different business logic in different packages to see how it feels.
Comments
Comments are powered by GitHub Discussions. Sign in with GitHub to join the discussion.