• 前置条件

    在开发之前,请确保

    介绍

    demo需涉及到app层的service接口类与其实现类。

    service 调用领域对象或服务来解决问题,应用层Service主要有以下特性:

    1. 负责事务处理,所以事务的注解可以在这一层的service中使用。
    2. 只处理非业务逻辑,重点是调度业务处理流程。业务逻辑处理一定要放在领域层处理。
    3. 不做单元测试,只做验收测试。
    4. 可能会有比较多的依赖组件(领域服务),使用field注入依赖的组件。
    5. 方法要求无状态,只接受dto元数据类型作为参数。

    Service 接口类

    UserService.java 代码

    package io.choerodon.todo.app.service;
    
    import io.choerodon.todo.api.dto.UserDTO;
    
    public interface UserService {
        UserDTO create(UserDTO userDTO);
    }
    

    TaskService.java 代码

    package io.choerodon.todo.app.service;
    
    import io.choerodon.todo.api.dto.TaskDTO;
    
    public interface TaskService {
    
        TaskDTO create(TaskDTO taskDTO);
    
        void deleteById(Long id);
    
        void deleteByTaskNumber(String taskNumber);
    
        TaskDTO update(TaskDTO taskDTO);
    
        TaskDTO queryById(Long id);
    }
    
    

    Service 实现类

    UserServiceImpl.java 代码

    package io.choerodon.todo.app.service.impl;
    
    // 省略 import
    
    @Service
    public class UserServiceImpl implements UserService {
        private UserRepository userRepository;
        private UserConvertor userConvertor;
    
        public UserServiceImpl(UserRepository userRepository, UserConvertor userConvertor) {
            this.userRepository = userRepository;
            this.userConvertor = userConvertor;
        }
    
        @Override
        public UserDTO create(UserDTO userDTO) {
            return userConvertor.entityToDto(
                    userRepository.create(userConvertor.dtoToEntity(userDTO))
            );
        }
    }
    

    UserServiceImpl.java 代码

    package io.choerodon.todo.app.service.impl;
    
    // 省略 import
    
    @Service
    public class UserServiceImpl implements UserService {
        private UserRepository userRepository;
        private UserConvertor userConvertor;
    
        public UserServiceImpl(UserRepository userRepository, UserConvertor userConvertor) {
            this.userRepository = userRepository;
            this.userConvertor = userConvertor;
        }
    
        @Override
        public UserDTO create(UserDTO userDTO) {
            return userConvertor.entityToDto(
                    userRepository.create(userConvertor.dtoToEntity(userDTO))
            );
        }
    }
    

    TaskServiceImpl.java 代码

    package io.choerodon.todo.app.service.impl;
    
    // 省略 import
    
    @Service
    public class TaskServiceImpl implements TaskService {
        private TaskRepository taskRepository;
        private TaskConvertor taskConvertor;
    
        public TaskServiceImpl(TaskRepository taskRepository, TaskConvertor taskConvertor) {
            this.taskRepository = taskRepository;
            this.taskConvertor = taskConvertor;
        }
    
        @Override
        public TaskDTO create(TaskDTO taskDTO) {
            return taskConvertor.entityToDto(
                    taskRepository.create(taskConvertor.dtoToEntity(taskDTO)));
        }
    
        @Override
        public void deleteById(Long id) {
            taskRepository.deleteById(id);
        }
    
        @Override
        public void deleteByTaskNumber(String taskNumber) {
            taskRepository.deleteByTaskNumber(taskNumber);
        }
    
        @Override
        public TaskDTO update(TaskDTO taskDTO) {
            return taskConvertor.entityToDto(
                    taskRepository.update(taskConvertor.dtoToEntity(taskDTO)));
        }
    
        @Override
        public TaskDTO queryById(Long id) {
            return taskConvertor.entityToDto(taskRepository.queryById(id));
        }
    }