DDD-Layered Architecture
Tags: rails, ddd, layeredarch
Published: 2022-11-02 14:23:44

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9eh3ftbwlkytxo73iry5.png)



## What’s the **DDD** ?

1. is a software design approach focusing on modeling software to match a domain according to input from the domain’s experts.

2. is to bridge the gap between domain experts and developers by using the same language to create the same understanding

3. the structure and language of software code (class names, class methods, class variables ) should match the business domain.

4. is very suitable for complex domains.

EX:  if a software processes food applications, it might have classes like Order, OrderPayment and Customer, and methods such as CreateOrder, AcceptOrder, checkout and.





## What’s the **Layered (N-Tier) Architecture** ?

1. The most common architecture pattern is the layered architecture pattern.

2. Components within the layered architecture pattern are organized into horizontal layers, each layer performing a specific role within the application.

3. 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.

4. (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:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssbvmtjijix8r95bsyeq.png)


DDD-layered architecture digram example:- 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qb0vrhy4i20xke13fcm2.png)



Project folder structure example :- 
(ruby on rails project)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwvtlqljtnqpg88vbkhu.png)




## 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.

- **Communication** 
    bridge the gap between domain business and developers.

- **Flexibility** 
    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 
       naturally.

- **Maintainability**


## The approach Cons:-


- Requires Domain Expertise.
- Requires Developers have the knowledge of the DDD and Layered arch.
- **Costly** 
    1. 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