javadomain-driven-designreactive-programmingquarkusddd-repositories

Doubts on how to use the reactive paradigm with DDD - Domain repositories


I am working on a project where the DDD methodology is going to be used for the development and, due to the business needs, a reactive programming approach is going to be used.

I am modeling the domain, and some doubts have arisen when I was working on the definition of the repositories. I see the problem when implementing the repositories, through an adapter, because using reactive programming, all methods return Uni or Multi (Mutiny! will be used). This is a problem because I am introducing dependencies with the framework in the domain, which violates DDD principles.

I have seen some posts on this forum (e.g.: DDD Java with Spring - Repository returning Mono/Flux), but I am not confident with the solution proposed. Has anyone been able to deal with this challenge effectively?


Solution

  • You can declare your domain repository interface with generic type:

    public interface MyRepositoryOutgoingPort <T> {
      T persist(My my);
    }
    

    And in reactive (e.g. Mongo) implementation you can wrap the result:

    @Repository
    public interface MyRepositoryMongoImpl
    extends ReactiveMongoRepository<My, UUID>,
        MyRepositoryOutgoingPort<Mono<My>> {
    
      @Override
      default Mono<My> persist(My my) {
        Mono<My> savedMy = save(my);
        return savedMy;
      }
    }