Domain driven design 在处理非幂等调用时是否应该使用事件设计?

Domain driven design 在处理非幂等调用时是否应该使用事件设计?,domain-driven-design,Domain Driven Design,我在做一个机票预订项目 下图显示了我们目前开发的领域模型 我们定义了一个域服务(AirBookService),它封装了预订、票务和其他操作。我们的供应商提供远程过程调用api来处理这些请求,因此我们通过添加反腐败层(我们有多个供应商)来实现域服务 此解决方案在处理无效rpc调用(如获取价格)时效果良好。但是,在处理非imdenpower rpc调用时存在风险 比如说 public class TransactionalReservationHandlingServiceImpl .... {

我在做一个机票预订项目

下图显示了我们目前开发的领域模型

我们定义了一个域服务(AirBookService),它封装了预订、票务和其他操作。我们的供应商提供远程过程调用api来处理这些请求,因此我们通过添加反腐败层(我们有多个供应商)来实现域服务

此解决方案在处理无效rpc调用(如获取价格)时效果良好。但是,在处理非imdenpower rpc调用时存在风险

比如说

public class TransactionalReservationHandlingServiceImpl .... {
    @Transactional
    @Override
    public void modifyTraveler(String resId, String tktId, AirTravler traveler) {
         AirReservation res = reservationRepository.findBy(resId);
         res.modify(tktId, traveler);
         airBookService.modify(res, traveler);
         reservationRepository.store(res);
    }
}
我将airBookService.modify()放在res.modify()后面,因此如果某些本地域逻辑被破坏,可以避免rpc调用。但是如果rpc调用成功而本地事务失败怎么办?我们的应用程序与供应商的应用程序之间存在差异

是否值得在单独的事务中处理rpc调用和本地修改

我关注的是:

a) It will surely introduce some extra complexity if doing so. like messaging. b) I don' have much experience in event handling. c) The failure chances are very low even if we use rpc call in the transaction boundary, mostly caused by concurrency problem and contetion of AirReservation is relatively low in real world. 其优点是本地修改与rpc调用分离。 但这带来了: 1.多资源管理问题(数据源和消息传递) 2.我必须为修改旅行者、点票和任何其他AirBookService操作创建大量临时事件

我左右为难,对当前的设计不满意,但对新的活动设计却犹豫不决


欢迎您提出任何意见,提前感谢。

在您的第一个示例中,您将本地修改与远程修改相结合。您担心,如果远程修改成功后本地修改失败,则无法再回滚远程修改。因此,将您的两个修改分离无疑是一条可行之路

最简单的方法是交换airBookService.modify调用和reservationRepository.store调用:

public class TransactionalReservationHandlingServiceImpl .... {
    @Transactional
    @Override
    public void modifyTraveler(String resId, String tktId, AirTravler traveler) {
         // local modification
         AirReservation res = reservationRepository.findBy(resId);
         res.modify(tktId, traveler);
         reservationRepository.store(res); // <- remember this should not actually store anything until after the commit

         // remote modification
         airBookService.modify(res, traveler);
    }
}
公共类TransactionalReservationHandlingServiceImpl。。。。{
@交易的
@凌驾
公共void modifyTraveler(字符串剩余、字符串tktId、AirTravler traveler){
//局部修改
AirReservation res=reservationRepository.findBy(resId);
res.modify(tktId,旅行者);
reservationRepository.store(res)//
public class TransactionalReservationHandlingServiceImpl .... {
    @Transactional
    @Override
    public void modifyTraveler(String resId, String tktId, AirTravler traveler) {
         // local modification
         AirReservation res = reservationRepository.findBy(resId);
         res.modify(tktId, traveler);
         reservationRepository.store(res); // <- remember this should not actually store anything until after the commit

         // remote modification
         airBookService.modify(res, traveler);
    }
}