Clean Architecture: Understanding the Role of Presenters
Erick Zanetti
Fullstack Engineer | Software Developer | React | Next.js | TypeScript | Node.js | JavaScript | AWS
Clean Architecture, proposed by Robert C. Martin, is an architectural approach aimed at creating modular, scalable, and maintainable systems. One of its main pillars is the clear separation between layers, where each has well-defined responsibilities. In this article, we will deeply explore the role of Presenters, one of the most important layers in the communication flow between the application layer and the user interface.
What Are Presenters in Clean Architecture?
Presenters are part of the interface layer, whose main responsibility is to prepare data to be displayed to the user in a way the interface can easily consume. They serve as intermediaries between use cases and the user interface, such as a web or mobile application.
In Clean Architecture, Presenters should:
Where Do Presenters Fit in Clean Architecture?
Clean Architecture is composed of four main layers:
Presenters act as a bridge between the use case and user interface layers. This separation ensures that changes in the interface or business flow do not directly impact each other.
Practical Example: Implementing a Presenter
Let’s assume we are developing a library system where users can browse a list of available books. The use case returns a list of Book objects with the following properties:
class Book {
constructor(title, author, publishedDate) {
this.title = title;
this.author = author;
this.publishedDate = publishedDate;
}
}
However, the user interface needs to display the books in a more readable format, such as “Book Title (Author) — Published in 10/2023.”
This is where the Presenter comes in, preparing the data. Here’s how it could be implemented in JavaScript:
领英推荐
Presenter
class BookPresenter {
present(books) {
return books.map(book => {
const publishedDate = new Date(book.publishedDate);
const formattedDate = `${publishedDate.getMonth() + 1}/${publishedDate.getFullYear()}`;
return `${book.title} (${book.author}) - Published in ${formattedDate}`;
});
}
}
Application Flow
The use case might return data like this:
class GetAvailableBooksUseCase {
constructor(bookRepository) {
this.bookRepository = bookRepository;
}
execute() {
return this.bookRepository.findAllAvailableBooks();
}
}
And the user interface would receive the formatted data as follows:
class BookController {
constructor(useCase, presenter) {
this.useCase = useCase;
this.presenter = presenter;
}
getAvailableBooks() {
const books = this.useCase.execute();
return this.presenter.present(books);
}
}
Benefits of Using Presenters
Common Mistakes When Working with Presenters
Conclusion
Presenters play a fundamental role in Clean Architecture by ensuring that data is presented in a clear, readable, and decoupled manner from the business logic. While they may seem like a simple abstraction, their proper implementation brings significant gains in maintenance, testability, and code reusability. When designing scalable systems, do not underestimate the power of a well-structured Presenter.
If you are new to Clean Architecture, start by implementing Presenters in small projects and notice how they help organize the code and simplify communication between layers. Happy coding!
Full Stack Software Engineer | React | Next.js | Node | Nest.js | Microsoft Azure certified
2 个月Nice post. I really like using using presenters in RESTful APIs because you have your presentation object independent of business model and swagger annotations are isolated in the web/controllers layer
Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)
2 个月Very helpful
.NET Developer | C# | TDD | Angular | Azure | SQL
2 个月Great advice and great content Erick Zanetti
.NET Software Engineer | Full Stack Developer | C# | Angular | AWS | Blazor
2 个月Useful, thanks Erick Zanetti