DDD中关于应用服务层建设的思考

2021-09-25  本文已影响0人  后厂村老司机

前言

应用服务层是Domain层的直接消费者,同时也是外部想要调用领域层的门面。
应用服务内部包含了翻译外部数据到领域对象的逻辑为领域服务准备领域对象的逻辑调用领域服务或者领域对象完成业务逻辑的编排逻辑翻译领域对象到外部数据的逻辑

一、应用服务如何做好翻译工作

1、层与层之间的参数传递

我们的服务中interface是领域层的直接消费者,interface层会从外部接收数据,然后调用application的方法。
由于服务是用CQRS模式实现的,所以对于领域对象的修改都是构建Command对象传递给application层
我们对interface层的controller入参直接使用了Command对象,然后交给application层处理,application层直接通过command的transfer方法翻译成对应的领域对象,调用领域层。


image.png

举个例子:

@PostMapping(value = "/labelGroup/create")
    public Response<String> createLabelGroup(@RequestBody LabelGroupCreateCommand labelGroupCreateCommand) {
        try {
            labelApplicationService.createLabelGroup(labelGroupCreateCommand);
        } catch (Exception e) {
            log.error("wrong:", e);
            return new Response<>(e.getMessage(), null);
        }
        return new Response<>(null);
    }
public class LabelApplicationService{
    @Override
    public void createLabelGroup(LabelGroupCreateCommand labelGroupCreateCommand) {
        LabelGroup labelGroupDomain = labelGroupCreateCommand.transfer();
        repository.save(labelGroupDomain);
    }
}

public class LabelGroupCommand {
    private Integer labelGroupId;
    private String labelGroupName;

    protected LabelGroup transfer() {
        return new LabelGroup(new LabelGroupId(labelGroupId), labelGroupName);
    }
}

2、command封装transfer()优势

上一篇 下一篇

猜你喜欢

热点阅读