Giter Club home page Giter Club logo

thin-ports-and-adapters's Introduction

thin-ports-and-adapters

GitHub Discussions

This is an example of a Ports and Adapters (P&A for short) architecture in Java 21 and Spring Boot.

We can define a simple P&A architecture as follows:

graph LR
    RestControllerAdapter --> ApplicationService
    ApplicationService --> Entity
    Entity --> RepositoryPort
    RepositoryPort --> RepositorySQLAdapter
    subgraph Outside 
        RestControllerAdapter
        RepositorySQLAdapter
    end
    subgraph Inside 
        ApplicationService
        Entity
        RepositoryPort
    end
    
    PAdapter(Primary Adapter) -.-> RestControllerAdapter
    PPort(Primary Port) -.-> ApplicationService
    SAdapter(Secondary Adapter) -.-> RepositorySQLAdapter
    SPort(Secondary Port) -.-> RepositoryPort

ports and adapters example Reference

A possible Spring Boot implementation of the adapters is:

// RestControllerAdapter (Primary Adapter)
@RestController
class TeamController {
    private final TeamService teamService; // Primary Port
    
    @GetMapping("/teams")
    public List<TeamData> getTeams() {
        return teamService.getTeams();
    }
}

// TeamRepositoryPort (Secondary Port)
interface TeamRepository extends ListCrudRepository<Team, Long> {
}

// The SQL Adapter of the TeamRepositoryPort is provided by Spring Data JPA. It is the Secondary Adapter.

Is it only this?

Yes, a ports and adapters architecture is only this.

Patterns like Domain Events, Aggregates, Value Objects, CQRS and Event Sourcing, all those patters go beyond the intention or responsibility of a ports and adapters architecture. Indeed, a P&A architecture works well with CRUD models (or models with not many domain logic) as well as Domain Models.

The beauty of it is that it adapts to each part of your business, and you can choose the right amount of abstraction/complexity for each scenario.

And testing?

IMHO, this is where a P&A shines. It really helps you create a solid and adaptable test suite. Here a short example of test types.

End-to-end tests

Integration test

Unit test

Running the application

./gradlew bootTestRun

โ„น๏ธ It uses Spring Boot integration with Testcontainers to spin up a PostgreSQL container.

Using the API

curl -X GET http://localhost:8080/teams
curl -X POST -H "Content-Type: application/json" -d '{"name": "Team 1"}' http://localhost:8080/teams
curl -X PATCH -H "Content-Type: application/json" -d '{"name": "Team 1"}' http://localhost:8080/teams/{id}/rename

Running the tests

./gradlew test

You will see how the thin ports and adapters architecture allows us to test the application with multiple approaches:

  • Unit tests for the domain logic.
  • Integration tests for the application service.
  • End-to-end tests for the REST API.

thin-ports-and-adapters's People

Contributors

aleixmorgadas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.