Skip to content

Adapters

The frameworks and drivers layer is where all the details go. The web is a detail. The database is a detail. We keep these things on the outside where they can do little harm.

©Robert C. Martin1

Adapters in the Machb Project are integral components that ensure the system adheres to the dependency inversion principle, a core concept of Clean Architecture. This principle dictates that high-level modules (core business logic) should not depend on low-level modules (such as frameworks, databases, or APIs). Instead, both should depend on abstractions.

In the Machb Project, adapters act as mediators between the core application logic (use cases and entities) and external systems (e.g., user interfaces, APIs, databases). They ensure the system remains modular, maintainable, and decoupled from implementation-specific details, making it easier to scale, replace, or evolve various parts of the system independently.

Be careful. Resist the temptation to commit the sin of knee-jerk elimination of duplication.

©Robert C. Martin

... when you are separating layers horizontally, you might notice
that the data structure of a particular database record is very similar to
structure of a particular screen view. You may be tempted to simply pass the
database record up to the UI, rather than to create a view model that looks the same
and copy the elements across. Be careful: This duplication is almost certainly
accidental. Creating the separate view model is not a lot of effort, and it will help
you keep the layers properly decoupled.

©Robert C. Martin

Project Structure

In the Machb Project, adapters are structured as modular components within the core layer to handle interactions with external systems, such as APIs, databases, or third-party services. Their role is to translate data formats and isolate implementation details, enabling clean, decoupled connections between the business logic and system integrations.

By utilizing adapters, the Machb Project ensures that changes in external systems (e.g., an API endpoint or a database schema) do not ripple through and impact the core business logic.

Principles of Adapters in Machb

  1. Maintain Dependency Inversion:
    Adapters prevent high-level policies (use cases) from depending directly on low-level details (frameworks, drivers). They invert the dependency by relying on abstractions, ensuring the flexibility and scalability of the system.

  2. Separation of Concerns:
    Cleanly decouples the implementation-specific details (e.g., how data is fetched or displayed) from the core business rules, keeping each layer focused on its purpose.

  3. Decoupled Data Transformation:
    Adapters handle the transformation of data between external systems and internal structures, such as converting database records to domain entities or API responses to core models.

  4. Enhances Testability and Maintainability:
    By isolating external dependencies, adapters make testing core business logic simpler and more robust, as mocks can easily replace external integrations during testing.

References

https://www.free-Counters.org