0% found this document useful (0 votes)
2 views

Clean Architecture - SA Guest lecture

Clean Architecture is a software design philosophy that emphasizes separation of concerns, allowing for easier management and maintenance of complex systems. It focuses on keeping business logic independent from external factors like frameworks and databases, promoting stability and testability. The document outlines the principles, layers, and benefits of implementing Clean Architecture, along with practical examples and considerations for its use.

Uploaded by

Obaid R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Clean Architecture - SA Guest lecture

Clean Architecture is a software design philosophy that emphasizes separation of concerns, allowing for easier management and maintenance of complex systems. It focuses on keeping business logic independent from external factors like frameworks and databases, promoting stability and testability. The document outlines the principles, layers, and benefits of implementing Clean Architecture, along with practical examples and considerations for its use.

Uploaded by

Obaid R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Clean

Architecture

Obaid ur Rehman
Software Architect / Engineering Manager @ Folio3
Who are you? 󰳓
⬢ Software Architect / Engineering Manager at
Folio3
⬢ Over 18+ years of experience
⬢ IBA: Taught Introduction to DevOps in Spring 2024
Agenda
● What is clean Architecture
● Theory of Clean Architecture
● Design Principles
● Implementation
● Real life example
● Benefits
● QA
What is Clean Architecture?

What is it exactly?
What is Clean Architecture?

An opinionated software design philosophy


that emphasizes the separation of concerns,
making it easier to manage, test, and
maintain complex software systems.
What is Clean Architecture?

Clean Architecture tries to ensure that the


most important parts of your application, like
business rules and logic, are independent of
external concerns such as frameworks,
databases, or user interfaces.

https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
History

Robert C. Martin aka Uncle Bob

Uncle Bob's blog - The Clean Architecture


Why Clean Architecture

Software Architecture pertains to the overall


structure and design of a system,
establishing how its parts interact and how
the system as a whole relates to external
entities.
Why Clean Architecture

The goal of software architecture is to minimize


the human resources required to build and
maintain the required system.
― Baba Robert C. Martin
n-Tier Architecture
Entities
● Entities represent the core business logic and rules of the
application. We can also think of them as enterprise-level
rules.

● They encapsulate the most general and high-level concepts


that remain constant, regardless of changes in the application
or technology. Entities are independent of frameworks,
databases, or any other external concerns.

● Entities are independent of frameworks, databases, or any


other external concerns.
Use Cases (Application-Specific Business Rules)
● Use cases define the specific actions or workflows of the
application. They orchestrate the interaction between entities
and other layers to fulfill user requirements.

● Their function is to encapsulate the business logic required for


specific tasks and ensure that rules and workflows remain
consistent.

● For example, a "Place Order" use case interacts with the Order
entity to validate items and calculate the total cost.
Interface Adapters

Interface Adapters bridge the core logic (Entities and Use Cases)
with external systems (databases, UI, or APIs).

They convert data formats between layers and keep business logic
unaware of external data structures.
Interface Adapters / Controllers
They receive HTTP @RestController
@RequestMapping("/orders")
requests, extract data, public class OrderController {
private final PlaceOrderUseCase placeOrderUseCase;
and call the
public OrderController(PlaceOrderUseCase placeOrderUseCase)
appropriate Use Case. {
this.placeOrderUseCase = placeOrderUseCase;
}
Example on right for a
@PostMapping
sample Controller public ResponseEntity<Map<String, Object>>
implementation. placeOrder(@RequestBody List<OrderItem> items) {
double total = placeOrderUseCase.execute(items);
Map<String, Object> response = Map.of("total", total);
return ResponseEntity.ok(response);
}

}
Interface Adapters / Gateways
Gateways abstract external systems like databases or APIs and
provide clean interfaces.

See the code example below where the OrderRepository


interface defines a clean contract for data persistence.

public interface OrderRepository {

void save(Order order);

}
Frameworks and Drivers
● This outermost layer includes the technical details of the
system, such as frameworks, databases, UI, and external APIs.

● The layer provides the infrastructure needed to support the


application (for example, databases, storage). This layer
contains no business logic but only implementation details for
interacting with external systems.

Example:

● A database driver for accessing stored data.


● A storage adapter to access cloud storage.
Design Principles of Clean Architecture

Single
Separation of Dependency
Responsibility
concerns Inversion
Principle

Persistence
Ignorance
Separation of concerns
The practice of dividing a software system into distinct sections,
each responsible for a specific aspect of the application:

● For example, it is considered a good practice when a web


controller handles HTTP requests, processes business logic,
and interacts directly with the database. ✅
Dependency Rule
The dependency rule
states that dependencies
should flow inward,
toward the system's core.

Outer layers such as


frameworks, UI, and
databases depend on
inner layers (business
logic and entities), but
not the other way
around.
Source: Design Application using Clean Architecture | by .Net Labs | Stackademic
Dependency Inversion → Benefits
● Changes in outer layers, like replacing a database, do not impact the core
business logic. Business rules remain stable even as technology evolves.

● Inner layers can be tested in isolation because they are not coupled with
external systems.

● Components like the user interface or database can be swapped with minimal
impact. For example, transitioning from a REST API to GraphQL affects only the
Interface Adapters layer.

● The system grows easily by adding new features or technologies without


disrupting the core logic.

● External systems (for example, web frameworks) are often volatile, with
frequent updates or deprecations. By isolating these changes, the core remains
unaffected.
Persistence Ignorance
Persistence Ignorance advocates for a clear separation between
the domain model and the infrastructure concerns such as data
storage. This principle posits that domain classes should solely
encapsulate the business logic and rules pertinent to the
application, without being polluted by technical details related to
persistence mechanisms.
Implementation of
Clean Architecture
in .NET
Source: Design Application using Clean Architecture | by .Net Labs | Stackademic
Domain Layer
● Entities
● Interfaces for Presentation

abstraction Application

● Exceptions
● Enums
Domain

Infrastructure
Application Layer
● Orchestrate the domain
● Business Logic Presentation

(Application) Application

● Use Cases (Features)


● Services or CQRS
Domain

Infrastructure
Infrastructure Layer
● Interface to External
Systems like: Presentation

● Email Application

● Storage
● Database
Domain
● Message Queues
● Cache

Infrastructure
Presentation Layer
● Entry point to system
● gRPC Presentation

● Rest API Application

This is where we do:


Domain
● Dependency Injection
● Middleware
● API Endpoints
Infrastructure
Clean Architecture

Presentation

Application

Domain

Infrastructure
Demo
In Conclusion
● When to use Clean Architecture
● Steep Learning Curve
● Performance Concerns
Questions?

You might also like