5

webflux 大佬求教,编程范式疑问

 2 years ago
source link: https://www.v2ex.com/t/803507
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  程序员

webflux 大佬求教,编程范式疑问

  myCupOfTea · 1 天前 · 544 次点击

webflux 是不是很容易写出回调地狱啊
话说原本很简单的一个场景,查询 User 和 UserInfo 的信息,然后复制给 OperInfo
webflux 写出来总感觉怪怪的

// mvc
User user = userRepository.findById(xxx);
UserInfo userInfo = userInfoRepository.findById(xxx);
OperInfo operInfo = new OperInfo();
BeanUtils.copyProperties(user, operInfo);
BeanUtils.copyProperties(userInfo, operInfo);
return operInfo;

我 webflux 写成了下面,不知道应该怎么写才是最佳实践

// webflux
Mono<User> user = userRepository.findById(xxx);
Mono<UserInfo> userInfo = userInfoRepository.findById(xxx);
return Mono.zip(user, userInfo).flatmap(data -> {
	User user = data.getT1();
	UserInfo userInfo = data.getT2();
	OperInfo operInfo = new OperInfo();
        BeanUtils.copyProperties(user, operInfo);
	BeanUtils.copyProperties(userInfo, operInfo);
        return operInfo;
})

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK