Domain-Centric Architecture Overview
Domain-Centric Architecture Overview
11/8/2024 2
What is Domain?
For example:
1 1..*
Customer Bank
1
*
1..* 1 1 1..*
Statement Account Transaction
type of
For example:
11/8/2024 5
Three-Tier Architecture’s dependency direction
11/8/2024 6
Three-Tier Architecture’s dependency direction
Dependency
11/8/2024 7
Problems with Data-Centric Architecture
For example:
11/8/2024 10
Database-Centric Architecture
11/8/2024 12
Domain-Centric Architecture
High-level modules should not depend on low-level modules. Both should depend
on abstractions.
Abstractions should not depend upon details. Details should depend upon
abstractions.
11/8/2024 13
Domain-Centric Architecture
Presentation
Let’s add an application layer as a façade for the system’s
public interface. It describes all the operations exposed by
the system.
Application
Now Business layer is broken down into Domain Layer and
Application Layer.
Business
Persistence
11/8/2024 14
Domain-Centric Architecture
11/8/2024 15
Domain-Centric Architecture
Now CarService in Domain layer will extract ICarRepository inside the application
layer and the implementation CarRepository is done inside the persistence layer.
If we decide to replace SQL Server database with MySQL, we must change or create a
new implementation in persistence layer.
11/8/2024 16
Database-Centric vs. Domain-Centric Architecture
(use cases)
DB Server
11/8/2024 17
Essential vs.Detail
Space is essential
Usability is essential
Decoration is a detail
11/8/2024 18
Essential vs.Detail
Domain is essential
(use cases)
Use cases (Business) is essential
Presentation is a detail
Persistence is a detail
Infrastructure is a detail
DB Server
11/8/2024 19
Database-Centric vs. Domain-Centric Architecture
Dependency
Dependency (use cases)
DB Server
11/8/2024 20
Chapter 6
11/8/2024 21
Modern Four-Layered Architecture
Presentation
Business
Persistence
11/8/2024 22
Modern Four-LayerModern Four-Layered Architectureed Architecture
User
• Similar to Three-Tier architecture
Presentation
• Application encapsulates use cases
Application
• Application layer dependent on Domain
Domain
• Domain layer contains business rules
Persistence Infrastructure
• Persistence layer gateway to database
Database OS
11/8/2024 23
Modern Four-LayerModern Four-Layered Architectureed Architecture
11/8/2024 24
Modern Four-Layered Architecture
Advantages
▪ Separation of concerns
▪ Easy to understand
injection)
11/8/2024 25
Modern Four-Layered Architecture
Disadvantages
▪ Add layer cost
11/8/2024 26
Domain-Driven Design
11/8/2024 27
4-Layered Architecture
User Interface / Presentation Layer
It is the interface for interactions with customers (e.g : Web UI, CLI, REST API).
Application / Service Layer Presentation
The service layer functions as a bridge between the
presentation layer and the business logic layer of the
application. Application
• Value Objects,
• Entities,
• Aggregates.
11/8/2024 29
Value Objects
Value objects have attributes, but can’t exist on their own. Value objects are immutable
objects that encapsulate the data. Value objects are immutable, meaning they cannot be
changed once created.
Value Objects
11/8/2024 30
Entities
Entities are a combination of data and behavior. It encapsulates the data and behavior of its
attributes. It may be convenient to view entities as a collection of other entities and value
objects that need to be grouped together.
11/8/2024 31
Aggregates
An aggregate has the following qualities:
11/8/2024 32
Aggregates (cont)
1. The CheckingAccount aggregate is
composed of child entities and value
objects.
11/8/2024 34
Types of Domain Centric Architecture
1. Hexagonal Architecture
2. Onion Architecture
3. Clean Architecture
DB
11/8/2024
Server
35
Hexagonal Architecture
▪ Introduced by Alistair Cockburn in
2005
11/8/2024 37
Hexagonal Architecture
• The communication between Inside
part and Outside part can happen
through Port and Adapters.
• Port: Interface
• controls a database
11/8/2024 40
Control Flow
On the “other side” of the application, the database
adapter could translate the “Save User” command into
an “INSERT INTO User VALUES (…)” SQL query
11/8/2024 41
Control Flow
Several adapters can be connected to one port.
For example, as in the example above, a user interface adapter and a REST adapter
can both be connected to the port to control the application
11/8/2024 42
Ports
Ports act as gateways for communication,
functioning as either inbound or outbound ports.
11/8/2024 43
Adapters
Adapters function as implementations of ports,
handling user input and converting it into language-
specific commands. They contain the logic for
interacting with databases, facilitating communication
between external entities and the core logic.
11/8/2024 45
Dependency Rule with Driving “Port-Adapter”
The RegistrationController is the adapter, the RegistrationUseCase is the interface, and
the RegistrationService in Domain layer implements the functionality described by the
port.
The source code dependency must be opposite to the invocation direction? How can
the application core access the database if the database is outside the core and the
source code dependency is to be directed to the center?
11/8/2024 47
Dependency Rule with Driven “Port-Adapter”
11/8/2024 48
Dependency Rule with Driven “Port-Adapter”
The PersistanceAdapter does not use the PersistencePort but implements it. And the
RegistrationService does not implement the PersistencePort but uses it
Employing the
dependency inversion
principle, we can choose
the direction of a code
dependency –opposite to
the calling direction.
11/8/2024 49
Benefits of Hexagonal Architecture
• Testability: Isolating the application and business logic from external factors so that
they can all be tested easily and separately.
• Application work without any technology: Designing the user interfaces by their
purpose rather than technology ensures that your application’s technology stack can
freely grow over time.
• Scalability: The ports and adapters are just as replaceable as all the external entities,
further contributing to the scalability of the entire application.
• Maintainability: The advanced separation of concerns also makes the app easier to
maintain, as changing the code in one place or adding new dependencies/ways to
interact with the app, do not require significant code changes.
11/8/2024 50
Onion Architecture
11/8/2024 51
Key Principles of Onion Architecture
11/8/2024 52
Layer Description
1. Domain Model.
• the innermost layer of the architecture.
• they represent the business models,
containing the business rules from it’s
domain.
//domain logics
public void withdraw(double amount);
public void deposit(double amount);
}
11/8/2024 53
Layer Description
2. Domain Services.
For example:
In banking system, Transfer feature involves 2
Accounts but it’s not clear if we implement it in
Account Model. So we will implement it in
Domain Services.
11/8/2024 56
Layer Description
4. Infrastructure Layer.
11/8/2024 57
Benefits of Onion Architecture
11/8/2024 58
Clean Architecture
11/8/2024 59
Clean Architecture’s Layers
1. Entities layer
11/8/2024 60
Clean Architecture’s Layers
2. Use Cases layer
11/8/2024 62
Clean Architecture’s Layers
1. Frameworks and Drivers layer
• so likely to change.
11/8/2024 63
Types of Domain Centric Architecture
and infrastructure
11/8/2024 64
Chapter 6
11/8/2024 65
Advantages of Domain-Centric Architecture
▪ More maintainable
11/8/2024 66
Advantages and Disadvantages
Disadvantages
Requires a change in thinking
11/8/2024 67