(原諒我,如果在科特林語法是錯誤的,如果我做在Java中風格的東西:O)
fun save(user: User): Mono<User> {
//we'll prepare several helpful Monos and finally combine them.
//as long as we don't subscribe to them, nothing happens.
//first we want to short-circuit if the user is found (by email).
//the mono below will onError in that case, or be empty
Mono<User> failExistingUser = findByEmail(user.email)
.map(u -> { throw new UserAlreadyExistsException(); });
//later we'll need to encode the password. This is likely to be
//a blocking call that takes some time, so we isolate that call
//in a Mono that executes on the Elastic Scheduler. Note this
//does not execute immediately, since it's not subscribed to yet...
Mono<String> encodedPassword = Mono
.fromCallable(() -> passwordEncoder.encode(user.password))
.subscribeOn(Schedulers.elastic());
//lastly the save part. We want to combine the original User with
//the result of the encoded password.
Mono<User> saveUser = user.toMono() //this is a Kotlin extension
.and(encodedPassword, (u, p) -> {
u.password = p;
return u;
})
//Once this is done and the user has been updated, save it
.flatMap(updatedUser -> userRepository.save(updatedUser));
//saveUser above is now a Mono that represents the completion of
//password encoding, user update and DB save.
//what we return is a combination of our first and last Monos.
//when something subscribes to this combination:
// - if the user is found, the combination errors
// - otherwise, it subscribes to saveUser, which triggers the rest of the process
return failExistingUser.switchIfEmpty(saveUser);
}
縮短的版本,而中介變量,也不評論:
fun save(user: User): Mono<User> {
return findByEmail(u.email)
.map(u -> { throw new UserAlreadyExistsException(); })
.switchIfEmpty(user.toMono())
.and(Mono.fromCallable(() -> passwordEncoder.encode(user.password))
.subscribeOn(Schedulers.elastic()),
(u, p) -> {
u.password = p;
return u;
})
.flatMap(updatedUser -> userRepository.save(updatedUser));
}
你應該解釋一下你的問題是什麼 – s1m0nw1
@ s1m0nw1我想反應的風格重構這個 – maystrovyy