Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing Domain Driven Design with Spring

Implementing Domain Driven Design with Spring

Talk given at Spring IO 2024 conference:

After years of Microservices hype, it seems that our industry now understands its costs and how difficult it is to build a distributed system. We are looking again towards building monolithic applications. But how can we develop monoliths that don’t turn into an unmaintainable mess? There is no single recipe, but we can say with a high degree of confidence that domain modeling techniques presented in Domain-Driven Design, in combination with Clean/Onion/Hexagonal Architecture, can significantly help in achieving clear and well-structured code aligned with the business needs.

In this talk, we will focus on the implementation aspects of DDD. You will understand what Aggregates, Value Objects, Repositories, Domain Events are, and how to implement them with Spring and other technologies from the Spring ecosystem. You will learn how to structure application services with Use Cases and how to leverage dependency inversion to separate technology-specific implementation details from the application and domain code.

GitHub repository: https://github.com/maciejwalkowiak/implementing-ddd-with-spring-talk

Maciej Walkowiak

June 04, 2024
Tweet

More Decks by Maciej Walkowiak

Other Decks in Programming

Transcript

  1. Domain-Driven Design is an approach to software development that centers

    the development on programming a domain model that has a rich understanding of the processes and rules of a domain. https://martinfowler.com/bliki/DomainDrivenDesign.html Martin Fowler
  2. UNDERSTAND THE DOMAIN DEVELOP A DOMAIN MODEL DEVELOP AN UBIQUITUS

    LANGUAGE SEPARATE DOMAIN MODEL FROM IMPLEMENTATION DETAILS SPLIT BIG DOMAIN INTO SUBDOMAINS
  3. Maciej Walkowiak | @maciejwalkowiak HTTP API (Spring MVC, Spring WebFlux)

    Persistence (JPA, JDBC, MongoDB … + @Repository) HTTP Clients (@HttpExchange, RestClient, OpenFeign) Caching (Redis, Caffeine, …) Business Logic (@Service) Messaging (Kafka, RabbitMQ, SQS …)
  4. Maciej Walkowiak | @maciejwalkowiak HTTP API (Spring MVC, Spring WebFlux)

    Persistence (JPA, JDBC, MongoDB …) HTTP Clients (@HttpExchange, RestClient, OpenFeign) Caching (Redis, Caffeine, …) Business Logic Messaging (Kafka, RabbitMQ, SQS …)
  5. Maciej Walkowiak | @maciejwalkowiak HTTP API (Spring MVC, Spring WebFlux)

    Persistence (JPA, JDBC, MongoDB …) HTTP Clients (@HttpExchange, RestClient, OpenFeign) Caching (Redis, Caffeine, …) Domain Model Messaging (Kafka, RabbitMQ, SQS …)
  6. Maciej Walkowiak | @maciejwalkowiak Domain Model DOMAIN MODEL ENCAPSULATES DOMAIN

    KNOWLEDGE, DOMAIN RULES, PROCESSES, CONSTRAINTS, BEHAVIOURS, STATE CHANGES.
  7. Maciej Walkowiak | @maciejwalkowiak Domain Model Entities Value Objects Repositorie

    s Domain Services Factories Aggregates TACTICAL DESIGN
  8. Maciej Walkowiak | @maciejwalkowiak As a librarian I want to

    register a copy of a book available to lend, by scanning book ISBN and the bar code. The bar code is printed in the library and pasted on each copy of the book. Each copy has its own unique bar code. If the book is not yet in the catalog, it should be added there with information about the title and ISBN. As a library user, I want to borrow a copy of a book. I want to know up until when I can hold the book so that I don't pay a fee. As a library user, I want to return a copy of a book I borrowed. If the book is returned after the return date, user must pay a fee. To avoid manual work, we are going to use 3rd party service that returns book information based on the ISBN code. For example: https://openlibrary.org/ We learned that there is already another company implementing a service that calculates the fee for late returns.
  9. Maciej Walkowiak | @maciejwalkowiak As a librarian I want to

    register a copy of a book available to lend, by scanning book ISBN and the bar code. The bar code is printed in the library and pasted on each copy of the book. Each copy has its own unique bar code. If the book is not yet in the catalog, it should be added there with information about the title and ISBN. As a library user, I want to borrow a copy of a book. I want to know up until when I can hold the book so that I don't pay a fee. As a library user, I want to return a copy of a book I borrowed. If the book is returned after the return date, user must pay a fee. To avoid manual work, we are going to use 3rd party service that returns book information based on the ISBN code. For example: https://openlibrary.org/ We learned that there is already another company implementing a service that calculates the fee for late returns.
  10. Maciej Walkowiak | @maciejwalkowiak • Each entity and value object

    can only exist in valid state • Ensure system-wide consistency through domain events published by aggregates • Business rules, state changes - implemented in entities • domain services - rules that do not belong to any entity Wrap Up Perhaps no domain services at all?
  11. Maciej Walkowiak | @maciejwalkowiak • JPA Entity may be a

    DDD entity (but does not have to) • Aggregates refer each other by identi fi er, not by a direct reference • Model Value Objects with records • Onion/Hexagonal/Clean Architecture works well with DDD technical patterns Wrap Up