Clean architecture recap
Clean architecture recap
Clean Architecture
UI
Core
Domain
1. Repository Interfaces
2. Entity Classes
Infrastructure
1. DbContext, Repositories
2. External API Calls
Clean Architecture
Allows you to change external systems (external APIs / third party services) easily,
without affecting the application core.
Scalable
You can easily scale-up or scale-out, without really affecting overall architecture of
the application.
Database independent
The application core doesn't depend on specific databases; so you can change it any
time, without affecting the application core.
Testable
The application core doesn't depend on any other external APIs or repositories; so
that you can write unit tests against business logic services easily by mocking
essential repositories.
Clean Architecture
Clean Architecture in ASP.NET Core
Core Layers
2. Application Layer
○ Purpose: Orchestrates the use cases of your application.
○ Contents:
■ Use Cases (Application Services): Implement the high-level use
cases or operations of your system (e.g., CreatePerson,
GetPersonById).
■ DTOs (Data Transfer Objects): Represent data structures used
for communication between layers.
■ Interfaces (Contracts): Define the contracts for infrastructure
services (e.g., repositories, email services).
○ Dependencies: Depends on the Domain Layer.
3. Infrastructure Layer
○ Purpose: Implements the technical details of how your application
interacts with external systems (databases, file systems, email
services, etc.).
○ Contents:
■ Repositories: Implement the data access logic for your entities.
■ Services: Implement the interfaces defined in the application
layer for interacting with external systems (e.g.,
EmailService, FileStorageService).
○ Dependencies: Depends on the Application Layer and any external
libraries or frameworks needed for infrastructure tasks.
5. Tests
○ Purpose: Ensures the correctness of your application's behavior.
○ Contents:
■ Unit Tests: Test individual units of code (e.g., domain models,
services) in isolation.
■ Integration Tests: Test the interaction between multiple
components.
■ End-to-End Tests: Test the entire application flow from the user's
perspective.
Dependency Direction:
● Inner Layers to Outer Layers: Dependencies flow from the inner layers
(Domain) to the outer layers (Presentation).
● Abstraction: Outer layers depend on abstractions (interfaces) defined in the
inner layers. This allows you to easily swap implementations in the outer
layers without affecting the core business logic.
// Application Layer
public class PersonDto { /* ... */ } // DTO for transferring person data
// Infrastructure Layer
public class PersonsRepository : IPersonsRepository
{
private readonly MyDbContext _dbContext;
public PersonsRepository(MyDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpPost]
public async Task<IActionResult> Create(PersonDto personDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Explanation:
● The Domain layer defines the core Person entity and the
IPersonsRepository interface.
● The Application layer defines the IPersonsService interface and the
PersonsService implementation that uses the repository to perform CRUD
operations.
● The Infrastructure layer contains the PersonsRepository that
implements the repository interface and interacts with the database.
● The Presentation layer has the PersonsController that handles
requests, uses the PersonsService, and returns appropriate responses.
Notes
Layers
1. Domain (Core):
○ Contains entities, value objects, and domain services.
○ Defines interfaces for repositories and other dependencies.
○ No external dependencies.
2. Application:
○ Contains use cases (application services) that orchestrate business
logic.
○ Defines DTOs (Data Transfer Objects) for communication between
layers.
○ Defines interfaces for infrastructure services (e.g., repositories).
○ Depends on the Domain layer.
3. Infrastructure:
○ Contains implementations of repositories, services for interacting with
external systems (e.g., email, database).
○ Depends on the Application layer and external libraries/frameworks.
4. Presentation (UI):
○ Contains controllers, views, and view models.
○ Handles user interaction and presentation logic.
○ Depends on the Application layer.
5. Tests:
○ Contains unit tests, integration tests, and end-to-end tests.
○ Ensures the correctness of each layer and the entire application.
Benefits
Interview Tips
● Explain the Layers: Be able to clearly explain the purpose of each layer and
how they interact.
● Dependency Direction: Emphasize that dependencies flow inwards towards
the Domain layer.
● Abstractions: Highlight the importance of using interfaces to achieve loose
coupling.
● Real-World Scenarios: Discuss how you've used or would use Clean
Architecture in a project.
● Benefits: Articulate the advantages of Clean Architecture in terms of
maintainability, testability, and flexibility.
Remember: