美文网首页
DDD中关于应用服务层建设的思考

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

作者: 后厂村老司机 | 来源:发表于2021-09-25 13:52 被阅读0次

    前言

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

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

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

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


    image.png

    举个例子:

    • interface层:常规spring controller操作
    @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);
        }
    
    • application层:
    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);
        }
    }
    
    • 注意application层的command和service位于同一个包下,transfer方法使用protected关键字限制只能在application层访问,即interface无法调用transfer方法。

    2、command封装transfer()优势

    • 上面的例子中对象创建的比较简单,如果有多层嵌套的树形command,那么transfer方法就极为好用,比如保存一个比较复杂的聚合,前端传过来一个聚合树command,里边很多值对象,那么每个值对象command都可以包含一个transfer方法,每次调用可以直接取。

    相关文章

      网友评论

          本文标题:DDD中关于应用服务层建设的思考

          本文链接:https://www.haomeiwen.com/subject/narxnltx.html