How to implement DDD as a software design approach with Layered Architecture in large projects?
First, I'm gonna introduce an important pattern in Domain Driven Design which's Bounded Context pattern. ✌️
What’s the Bounded Context?
Bounded Context is a central pattern in Domain-Driven Design. It is the focus of DDD's strategic design section which is all about dealing with large models and teams. DDD deals with large models by dividing them into different Bounded Contexts.
For Example in the E-commerce food apps we have Order context, payment context, menu context, delivery context, checkout context, subscription context, and every context is a large model and has a boundaries and each model can work independently.
In the future,
you can easily move each context to a microservice.
What’s the DDD ?
is a software design approach focusing on modeling software to match a domain according to input from the domain’s experts.
is to bridge the gap between domain experts and developers by using the same language to create the same understanding
the structure and language of software code (class names, class methods, class variables ) should match the business domain
EX: if a software processes food applications, it might have classes like Order, OrderPayment and Customer, and methods such as CreateOrder, AcceptOrder, checkout and.
- is very suitable for complex domains.
What’s the Layered (N-Tier) Architecture ?
The most common architecture pattern is the layered architecture pattern.
Components within the layered architecture pattern are organized into horizontal layers, each layer performing a specific role within the application.
The layered architecture pattern does not specify the number and types of layers that must exist in the pattern, most layered architectures consist of four standard layers.
(The layers of isolation concept ) Changes made in one layer of the architecture generally don’t impact or affect components in other layers.
Layered architecture digram example:-
DDD-layered architecture digram example:-
Project folder structure example :-
(ruby on rails project)
Why DDD and Layered Arch ?
I am now working on an e-commerce application (Food Industry)
And I was mainly working in the Order domain functionality, when we needed to change something in the code or add a new feature which was difficult and impossible because:-
- Code complexity.
- Inability to effectively debug.
- Development speed issue.
- Legacy and unstructured code.
- Order was tightly coupled with payments and delivery.
- There is a gap between the business and the codebase.
- Missing the single responsibility principle.
- There is no single source of truth.
- Identifying layers responsibilities.
- Readability issue.
We needed to isolate the field with known boundaries
To make our life easy in code.
The approach Pros:-
very suitable for complex domains.
The layers/domains of isolation concept.
bridge the gap between domain business and developers.
Since the system was built to model the
business domain, it will generally be more flexible to
change as new functional requirements would fit in
The approach Cons:-
- Requires Domain Expertise.
- Requires Developers have the knowledge of the DDD and Layered arch.
- This often results in a longer development and duration that ultimately translates to higher costs for the business, 2.Therefore it isn’t suited for short-term projects or projects where there isn’t a high domain complexity.
HINT: I will add a POC project using ruby on rails later
Top comments (4)
Great article, I hope more engineers start to focus on software architecture and cleanliness.
If anyone is interested in using the above in Node, here is my open source node-clean-architecture template.
Thanks bro <3
and this is a great template really i liked
That was very simple and useful 👌 good job 👏
thank you bro <3