DDD(领域驱动设计)思想解读及优秀实践吾爱fen享

前言: download:DDD(领域驱动设计)思想解读及优秀实践

下载课程ZY:https://www.97yrbl.com/t-1317.html

开发环境 jdk1.8【jdk1.7以下只能部分支持netty】
springboot 2.0.6.RELEASE
idea + maven
五、代码示例
itstack-demo-ddd-01 └── src ├── main │├── java ││└── org.itstack.demo ││├── application │││├── event ││││└── ApplicationRunner.java │││└── service │││└── UserService.java ││├── domain │││├── model ││││├── aggregates │││││└── UserRichInfo.java ││││└── vo ││││├── UserInfo.java ││││└── UserSchool.java │││├── repository ││││└── IuserRepository.java │││└── service │││└── UserServiceImpl.java ││├── infrastructure │││├── dao ││││├── impl │││││└── UserDaoImpl.java ││││└── UserDao.java │││├── po ││││└── UserEntity.java │││├── repository ││││├── mysql │││││└── UserMysqlRepository.java ││││├── redis │││││└── UserRedisRepository.java ││││└── UserRepository.java │││└── util │││└── RdisUtil.java ││├── interfaces │││├── dto ││││└── UserInfoDto.java │││└── facade │││└── DDDController.java ││└── DDDApplication.java │├── resources ││└── application.yml │└── webapp │└── WEB-INF │└── index.jsp └── test └── java └── org.itstack.demo.test └── ApiTest.java

application/UserService.java | 应用层用户服务,领域层服务做具体实现 /** * 应用层用户服务 * 虫洞栈:https://bugstack.cn * 公众号:bugstack虫洞栈 | 欢迎关注并获取更多专题案例源码 * Create by fuzhengwei on @2019 */ public interface UserService {UserRichInfo queryUserInfoById(Long id); }

domain/repository/IuserRepository.java | 领域层资源库,由基础层实现
/**
  • 虫洞栈:https://bugstack.cn
  • 公众号:bugstack虫洞栈 | 欢迎关注并获取更多专题案例源码
  • Create by fuzhengwei on @2019
    */
public interface IUserRepository {
void save(UserEntity userEntity); UserEntity query(Long id);

}
domain/service/UserServiceImpl.java | 应用层实现类,应用层是很薄的一层可以只做服务编排
/**
  • 虫洞栈:https://bugstack.cn
  • 公众号:bugstack虫洞栈 | 欢迎关注并获取更多专题案例源码
  • Create by fuzhengwei on @2019
    */
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource(name = "userRepository") private IUserRepository userRepository; @Override public UserRichInfo queryUserInfoById(Long id) {// 查询资源库 UserEntity userEntity = userRepository.query(id); UserInfo userInfo = new UserInfo(); userInfo.setName(userEntity.getName()); // TODO 查询学校信息,外部接口 UserSchool userSchool_01 = new UserSchool(); userSchool_01.setSchoolName("振华高级实验中学"); UserSchool userSchool_02 = new UserSchool(); userSchool_02.setSchoolName("东北电力大学"); List userSchoolList = new ArrayList<>(); userSchoolList.add(userSchool_01); userSchoolList.add(userSchool_02); UserRichInfo userRichInfo = new UserRichInfo(); userRichInfo.setUserInfo(userInfo); userRichInfo.setUserSchoolList(userSchoolList); return userRichInfo; }

}
infrastructure/po/UserEntity.java | 数据库对象类
/**
  • 数据库实体对象;用户实体
  • 虫洞栈:https://bugstack.cn
  • 公众号:bugstack虫洞栈 | 欢迎关注并获取更多专题案例源码
  • Create by fuzhengwei on @2019
    */
public class UserEntity {
private Long id; private String name; get/set ...

}
infrastructrue/repository/UserRepository.java | 领域层定义接口,基础层资源库实现
/**
  • 虫洞栈:https://bugstack.cn
  • 公众号:bugstack虫洞栈 | 欢迎关注并获取更多专题案例源码
  • Create by fuzhengwei on @2019
    */
@Repository("userRepository")
public class UserRepository implements IUserRepository {
@Resource(name = "userMysqlRepository") private IUserRepository userMysqlRepository; @Resource(name = "userRedisRepository") private IUserRepository userRedisRepository; @Override public void save(UserEntity userEntity) { //保存到DB userMysqlRepository.save(userEntity); //保存到Redis userRedisRepository.save(userEntity); }@Override public UserEntity query(Long id) {UserEntity userEntityRedis = userRedisRepository.query(id); if (null != userEntityRedis) return userEntityRedis; UserEntity userEntityMysql = userMysqlRepository.query(id); if (null != userEntityMysql){ //保存到Redis userRedisRepository.save(userEntityMysql); return userEntityMysql; }// 查询为NULL return null; }

}
interfaces/dto/UserInfoDto.java | DTO对象类,隔离数据库类
/**
  • 虫洞栈:https://bugstack.cn
  • 公众号:bugstack虫洞栈 | 欢迎关注并获取更多专题案例源码
  • Create by fuzhengwei on @2019
    */
public class UserInfoDto {
private Long id; // IDpublic Long getId() { return id; }public void setId(Long id) { this.id = id; }

}
interfaces/facade/DDDController.java | 门面接口
/**
@Controller
public class DDDController {
@Resource(name = "userService") private UserService userService; @RequestMapping("/index") public String index(Model model) { return "index"; }@RequestMapping("/api/user/queryUserInfo") @ResponseBody public ResponseEntity queryUserInfo(@RequestBody UserInfoDto request) { return new ResponseEntity<>(userService.queryUserInfoById(request.getId()), HttpStatus.OK); }

}
六、综上总结 【DDD(领域驱动设计)思想解读及优秀实践吾爱fen享】以上基于DDD一个基本入门的结构演示完成,实际开发可以按照此模式进行调整。
目前这个架构分层还不能很好的进行分离,以及层级关系的引用还不利于扩展。
后续会持续完善以及可以组合搭建RPC框架等,让整个架构更利于互联网开发。

    推荐阅读