Video in progress

In a reactive (WebFlux) project, should domain methods return `Mono`/`Flux`? What happens when framework types leak into the domain?

Usually no. Domain decisions should be pure and synchronous: values in, a result or an event out. Mono/Flux belong only where there's I/O: in the application service and adapters. If Reactor gets into the domain, business rules become much harder to read, to test and to move to virtual threads later.

📖 Answer

Code where Reactor has entered the domain:

public Mono<Order> applyDiscount(Mono<Order> order, Mono<Customer> customer) {
    return Mono.zip(order, customer)
        .flatMap(t -> t.getT2().isVip()
            ? Mono.just(t.getT1().withDiscount(10))
            : Mono.just(t.getT1()));
}

The rule is one line ("10% for VIPs"), but it's buried inside operators. Testing needs StepVerifier. The error stack trace comes from inside Reactor.

The separated version:

// domain: pure, synchronous
public Order applyDiscount(Order order, Customer customer) {
    return customer.isVip() ? order.withDiscount(10) : order;
}

// application service: reactive orchestration
public Mono<Order> discount(OrderId id) {
    return Mono.zip(orders.find(id), customers.findByOrder(id))
        .map(t -> pricing.applyDiscount(t.getT1(), t.getT2()))
        .flatMap(orders::save);
}

The rule is checked with a plain JUnit test, and reactivity stays at the I/O boundary.

The same idea applies to other framework types: ResponseEntity, HttpServletRequest, Kafka's ConsumerRecord and JPA's Page should not appear in the signature of domain methods. The domain API is in domain language.

There's a strategic reason too: with Java 21 virtual threads, many teams are going back from the reactive stack to plain blocking code. If the domain isn't tied to Reactor, that move only changes the adapters and services; if it is, all the business logic gets rewritten.

An exception: if the domain is naturally a stream (processing a price stream in real time), the stream abstraction can be a domain concept itself. Even then, it's better to expose your own small interface than Reactor.