初识 DDD

1 minute read

看了一篇使用 go 语言实践 DDD 的文章,内容比较浅,但也了解了一些基本的概念。

首先是 DDD 的基本架构:四个层级。

Architecture

  1. Interface: This layer holds everything that interacts with other systems, such as web services, RMI interfaces or web applications, and batch processing frontends.
  2. Application: This layer serves as a passage between the domain and the interface layer. The sends the requests from the interface layer to the domain layer, which processes it and returns a response. 这一层通常比较“薄”。
  3. Domain: This is where the domain and business logic of the application is defined.
  4. Infrastructure: This layer consists of everything that exists independently of our application: external libraries, database engines, and so on.

具体到上述文章和相应的项目代码,有这么一些补充:

(1)domain layer 有几种模式:

  • entity
  • value
  • repository
  • sevice

其中,entity,repository,service 我是有所了解和实践的,之前写 SpringBoot 程序经常要和这几个概念打交道,基本可以称作是标准实践形式了。

entity 中定义了数据结构模型,在这里是 User 和 Food,以及它们的一些 helper 函数。 repository 中定义了操作数据模型的方法,但是没有具体实现。这些方法和数据库或者第三方 API 交互,其具体实现在 infrastructure layer。

(2)infrastructure layer 包含了 repository 中接口方法的具体实现。

(3)application layer connects the domain and the interfaces layers. 这个例子中,application layer 就是将 domain layer 中的 repository 方法封装了一下。 类似于 SpringBoot 中的各种 service。

(4)interfaces layer handles HTTP requests and responses。类似于 controller 层。

这个项目并没有太多地触及 DDD 的核心,带给我的输入更多的是怎么围绕 “domain” 组织项目结构以及 DDD 的基本架构组成。感觉文章评论里这段话挺有道理:

The article focuses on project structure and layers like it is some stiff guideline for DDD whereas it is not. DDD is about describing the domain in the code. Then building upon this domain code to create some useful use cases (also dropping in some infrastructural code for persistence, messaging and stuff like that). DDD is about direction of dependencies and direction of control in those mentioned “layers”. About loosely coupled layers. It is about how your application can grow around the domain. In application that follows DDD principles, I should be able to tell what is the application about just looking at the packages.

延伸阅读

Tags: ,

Categories:

Updated:

Comments