I am new to reactive programming and to get my hand on I am trying to build a near to real example.
When you see reactor tutorials they show you very easy examples like.
return userRepository.findById(1);
or something like dealing with flux the break the "brown little fox" string and find unique letters etc. But mostly these tutorials stick to single object and unfortunately i am unable to find any guide lines or tutorial which show a side by side examples to type same code first in imperative then in reactive, thats why i see lots of new comers in reactive programming faces a lot of learning issues.
but my point is in real life applications we deals with multiple objects like below sample code I wrote in reactor. Apologies for bad code i am still learning.
public Mono<ServerResponse> response(ServerRequest serverRequest) {
return
Mono.just(new UserRequest())
.map(userRequest -> {
Optional<String> name = serverRequest.queryParam("name");
if (name.isPresent() && !name.get().isEmpty()) {
userRequest.setName(name.get());
return userRequest;
}
throw new RuntimeException("Invalid name");
})
.map(userRequest -> {
Optional<String> email = serverRequest.queryParam("email");
if (email.isPresent() && !email.get().isEmpty()) {
userRequest.setEmail(email.get());
return userRequest;
}
throw new RuntimeException("Invalid email");
})
.map(userRequest -> {
userRequest.setUuid(UUID.randomUUID().toString());
return userRequest;
})
.flatMap(userRequest ->
userRepository
.findByEmail(userRequest.getEmail())
.switchIfEmpty(Mono.error(new RuntimeException("User not found")))
.map(user -> Tuples.of(userRequest, user))
)
.map(tuple -> {
String cookiePrefix = tuple.getT2().getCode() + tuple.getT1().getUuid();
return Tuples.of(tuple.getT1(), tuple.getT2(), cookiePrefix);
})
//Some more chaining here.
.flatMap(tuple ->
ServerResponse
.ok()
.cookie(ResponseCookie.from(tuple.getT3(), tuple.getT2().getRating()).build())
.bodyValue("Welcome")
);
}
consider above code first i started with UserRequest object to map querystring in this object. then i need some data from database and so on reactive chaining continue more works to do. Now consider
- UserRequest Object from first chaining method and
- User document i fetched from db then i do lot more operations but at the end of chaining i need both of these objects to process final response. The only way to achieve that i found on google is Tuple. but the code look like more dirty after that since in every next operator i have to do
tuple.getT()
tuple.getT2()
So finally i would like to ask is that the proper way or i am missing something here. Because i learned one thing in reactive that data flows nothing more but like in imperative in the middle of logic we got oh i need another variable/object so i define it on top and use it but in reactive after 5th or 6th operator when developer realize ohh i need that object too here that was i created in 2nd operator then i have to go back and pass that in chaining to get in my 5th or 6th operator is that a proper way to do that.