DDD - In my dream, can I still let you bully me?

There is such a plot in "Charlotte Troubles": Charlotte traveled to his junior high school class. When he found himself in a dream, he watched his teacher who had been sarcastic and sarcastic saying a classic line: " In my dream, can I still let you bully me?" . He can say such "man" words because this is his dream, his "domain", and he is the "king" in this dream.

Then there is also the concept of domain in DDD, and the students in the team are also the "kings" in the domain they are responsible for. Through the domain, we will introduce two other concepts, namely: subdomain and bounding text . So, in today's article, let's talk about their stories.

1. Field

Since the meaning of DDD is "domain-driven design", then, what is a domain (Domain)?

In a broad sense, a domain is what an organization does and everything contained within it. In other words, a company or organization, its scope of business and the activities carried out within it .

The above definition may be a bit jerky, so let's take the very popular "Douluo Dalu" as an example: Tang San obtained the Killing God Domain and the Blue Silver Domain . Then, after displaying the power of the domain, one's own strength in the domain is greatly increased, while the strength of the opponent is greatly weakened.

Then the company is the same. Let's take Li Ning and Ali as examples. In the field of sportswear, Li Ning is far stronger than Ali; but in the field of e-commerce and Internet, Ali is stronger than Li Ning. So, in the vernacular to explain the concept of the word domain, it is - the circle that you are running, active, and good at .

The meaning of the word "domain" is diverse. It can not only represent the entire business system , but also a certain " core domain " or " supporting sub-domain ".

Since the "domain model" includes the word "domain", we may think that a single, complete, cohesive, fully functional model should be created for the entire business system, just like the Saint Seiya in "Saint Seiya". Just like clothing, it is complete, cohesive, versatile and powerful.

Then, on the contrary, in DDD, a domain is divided into several subdomains, and the domain model is developed in a bounded context . In fact, when developing a domain model, we usually focus on only one aspect of the business system —a subdomain. Regardless of the complexity of the software system itself, almost all software domains contain multiple subdomains.

2. Subdomains

According to different types, subdomains can be divided into core domains , support subdomains and general subdomains . Take a music website or app as an example (excluding copyright issues, assuming that all digital music major music websites can be played equally), it generally contains a lot of functions, we only focus on music taste recommendation, membership and permissions, and promotional activities for the time being. 3 parts of the function, then the following is the division of the subdomain types for these 3 parts:

【explain】

  • Then if we want our music website or app to stand out and be loved by the public, music taste recommendation will be the core ability. I have listened to a few songs, and I don’t know what songs to listen to later, but the website recommended it to me. The songs are particularly in line with my taste in music, so users naturally prefer to come here to listen to songs, so **"music taste recommendation" is the core domain**;
  • In fact, each function of the website will be used more or less with the ability to have permissions and permissions, so **"membership and permissions" is a general subdomain**;
  • 而“促销活动”这部分也属于业务能力的一部分,但是并没有核心域那么重要,“促销活动”便是支撑子域

2.1> 核心域

它是整个业务领域的一部分,也是业务成功的主要促成因素。从战略层面上讲,企业应该在核心域上胜人一筹。我们应该给予核心域最高的优先级、最资深的领域专家和最优秀的开发团队。在实施DDD的过程中,你将主要关注于核心域。

2.2> 支撑子域

如果某个限界上下文对应着业务的某些重要方面,但却不是核心,那么它便是一个支撑子域。创建支撑子域的原因在于它们专注于业务的某个方面。

2.3> 通用子域

如果一个子域被用于整个业务系统,那么这个子域便是通用子域。

三、限界上下文

运用限界上下文(BoundedContext)的战略设计模式来分离领域模型。一个领域被分为若干个子域,领域模型在限界上下文中完成开发

在实施DDD的时候,我们要保证每一个术语应该仅表示一种领域概念,即:将用到的每一个术语进行限界划分。这种基于语言层面上的上下文边界划分,也是实现DDD的关键。如下所示,同样的“售卖”一词在不同的上下文中含义都是不一样的:

限界上下文是一个显式的边界,领域模型便存在于这个边界之内。领域模型把通用语言表达成软件模型。创建边界的原因在于,每个模型概念,包括它的属性和操作,在边界内都具有特殊的含义。

上面我们介绍限界上下文的时候,一直再提边界的问题,边界真的这么重要?没有边界,不是一样不影响我们项目的开发迭代吗? 其实不然,在我们试图创建一个“大而全”的软件模型的时候,要使所有人都对某个概念的定义达成一致几乎不可能。因此,最好的方法是去正视这种不同,然后使用限界上下文对领域模型进行分离。可以举个例子,在广场上有一群人要商量去哪里,大家各抒己见,无法达成一致。

那怎么办呢?大家谁都不让步。分析原因,广场上的这群人,彼此之间又都是陌生的,那么陌生人也不会轻易的迁就对方,那么我们以家庭进行分组(建立边界),由一家人(即:在同一限界上下文中)内部去商量要去哪里,确定一致性建议。

1

由上面的例子我们可以看到,将不同的家庭分界开来,由于一家人是有血缘关系的,所以更容易达成一致。而在DDD中的这种“一家人”,就属于限界上下文,它把一个领域(类比:广场)划分为多个限界上下文,在限界上下文中进行领域建模,同时,这样更容易对某个概念的定义达成一致,建立专属这个限界上下文的领域语言。

四、战略设计的重要性

DDD是分为两大部分内容的:宏观上的——战略设计微观上的——战术设计。而在上面我们介绍的“领域”、“子域”和“限界上下文”其实都属于战略设计。那么在实施DDD的过程中,我们也需要遵守先执行战略设计,再执行战术设计的方式。那么,战略设计为什么这么重要呢?

其实一提到战略和战术,大部分同学的第一反应应该是在战场上高频出现的词汇,那么我们就以战场来做解释。下面是辽沈战役攻打锦州的作战部署图:

从上面的图中我们可以看到,分为了“2C”、“7D”、“8D”、“8C”等作战部队,并且针对每个部队都标注着要攻打的位置和路线。在这张图里,并没有标注用什么枪、用什么炮、用什么手榴弹、多少医护人员……也就是说,不包含打仗的具体细节(战术模式),只在宏观上(战略模式)划分的队伍和攻打方向。如果没有这个宏观的作战图,那么部队将会一盘散沙,各自为战,没有任何配合可言了。这个跟我们DDD中的战略模式是类似的。如下是对应关系:

  • 领域——>攻打锦城
  • 子域——>部队
  • 核心域——>主力部队,比如:8D
  • 支撑域——>其他非主力部队
  • 通用域——>通信部队,医疗部队,炊事班
  • 限界上下文——>某部队负责攻打的区域

通过上面针对战争的例子来看,战略的重要性远不亚于战术,就像人们常常说的那句话——“方向不对,努力白废!”,而战略设计就是DDD中的方向。

五、问题空间&解决方案空间

领域中还同时存在问题空间解决方案空间。它们的含义如下所示:

软件开发过程,本质上可以看作是问题空间到解决方案空间的一个映射转化。

5.1> 问题空间

在实际项目中,我们可以针对如下问题,对问题空间进行评估

  • 这个核心域的名字是什么?目标是什么?包含哪些概念?
  • 支撑子域和通用子域是什么?
  • 如何安排项目人员?能否组件一只合适的团队?

5.2> 解决方案空间

解决方案空间在很大程度上受到现有系统和技术的影响。在实际项目中,我们可以针对如下问题,对解决方案空间进行评估

  • 有哪些软件资产是存在的?是否可以重用?是否需要创建?是否可以从别的地方获得到?
  • 这些资产是如何集成起来的?需要如何集成?
  • 假设资产都已经ok了,我们还需要做什么?
  • What is the probability of success for the core domain and supporting projects? Will it be possible to lose all of them due to the failure of one of them? Where do we use completely different terms?
  • Where is there conceptual overlap between Bounded Contexts? How do these overlapping concepts map and translate between different bounded contexts?
  • Which Bounded Contexts contain concepts in the core domain, and which tactical patterns in [Evans] are used?

For more interesting DDD articles, please pay attention to "Java Muse"\(^o^)/~ "Dry goods sharing, weekly update"

Guess you like

Origin juejin.im/post/7116769446244384775