简体   繁体   English

我们应该在 DDD 中使用“按功能打包”结构吗?

[英]Should we use 'package by feature' structure with DDD?

After doing some research, I got confirmation that folder-by-feature structure is superior to folder-by-layer structure in the majority of cases.在做了一些研究之后,我确认在大多数情况下,逐特征结构优于逐层结构。 To get some arguments we can read, among others, the following articles or even this answer .为了获得一些论点,我们可以阅读以下文章甚至 这个答案

Package by features, not layers 按功能打包,而不是按层打包
Feature folders vs Tech folders 功能文件夹与技术文件夹
Package by feature, not layer按功能打包,而不是按层打包
Package by Layer for Spring Projects Is Obsolete Spring 项目逐层打包已过时

However, all the DDD project examples that I found are made with package-by-layer , most of the time following a structure like:但是,我发现的所有 DDD 项目示例都是使用package-by-layer 制作的,大部分时间都遵循如下结构:

├── application ├── 应用
├── config ├── 配置
├── domain ├── 域名
├── infrastructure ├──基础设施
└── interfaces └── 接口

So my question is: why the DDD community does not follow package-by-feature even if it's apparently superior in the majority of cases?所以我的问题是:为什么 DDD 社区即使在大多数情况下明显优越,也不遵循按功能打包的功能

Should we use package-by-feature with DDD?我们应该在 DDD 中使用按功能打包吗? If so, how to do it?如果是这样,该怎么做?

I mention that I'm not talking about the particular case of micro-service architecture where obviously package-by-layer is more relevant.我提到我不是在谈论微服务架构的特殊情况,其中显然逐层包更相关。

My understanding and my vision of this subject is as follows:我对这个课题的理解和看法如下:

Package by feature is about vertical slicing (structuring the source code according to domain concepts) instead of by horizontal layering (structuring according to technical concepts).按功能打包是关于垂直切片(根据领域概念构建源代码)而不是水平分层(根据技术概念构建)。

But saying "instead of" isn't completely true, since there's a moment when you have to distinguish between those technical concepts.但是说“代替”并不完全正确,因为有时您必须区分这些技术概念。 It would be better to say package by feature first, and then package by layer inside each feature.最好先按功能打包,然后在每个功能内部逐层打包。

In strategic DDD, each Bounded Context (BC) is a vertical slice (a whole application, full stack... with UI, application, domain model and infraestructure layers).在战略 DDD 中,每个有界上下文 (BC) 都是一个垂直切片(一个完整的应用程序、完整的堆栈……具有 UI、应用程序、域模型和基础架构层)。

Then, inside a BC, tactical DDD promotes packaging the domain model code by business concepts first, and then by technical concepts (tactical patterns).然后,在BC内部,tactical DDD提倡先按业务概念,再按技术概念(tactical patterns)打包领域模型代码。 So you have modules (groups of aggregates that are tightly cohesive and loosely coupled), and inside each aggregate you have entities, value objects, factories, repository.所以你有模块(紧密结合和松散耦合的聚合组),并且在每个聚合内部你有实体、值对象、工厂、存储库。 The other layers (UI, application, infra) can be structured by modules as well.其他层(UI、应用程序、基础结构)也可以由模块构成。

So as a summary DDD does follow a mixed approach:因此,作为总结 DDD 确实遵循混合方法:

  • Packaging by business concepts with different levels of granularity: BCs, modules, aggregates.按不同粒度级别的业务概念打包:BC、模块、聚合。

  • Package by layer inside a BC: UI, application, domain, infraestructure.在 BC 中逐层打包:UI、应用程序、域、基础设施。

PD: this subject (source code structure) is explained in Chapter 9 (Modules) of Vaughn Vernon's book "Implementing DDD". PD:这个主题(源代码结构)在 Vaughn Vernon 的书“Implementing DDD”的第 9 章(模块)中有解释。

why the DDD community does not follow package-by-feature为什么 DDD 社区不遵循按功能打包

I think you'll find that the good projects do follow package-by-feature.我想你会发现好的项目确实遵循按功能打包的。

In examples , you may find that it isn't being followed.examples 中,您可能会发现它没有被遵循。 The generous interpretation is that the authors are trying to make it easier to recognize the separation of concerns and the directions of the dependency arrows.慷慨的解释是,作者试图使人们更容易识别关注点的分离和依赖箭头的方向。

Less generously?不那么慷慨? the authors aren't paying attention, or don't know better.作者没有注意,或者不知道更好。

Should we use package-by-feature with DDD?我们应该在 DDD 中使用按功能打包吗? If so, how to do it?如果是这样,该怎么做?

Yes, and pretty much the same way you would do package-by-feature if you weren't doing DDD.是的,如果您不使用DDD,那么您将按功能打包的方式几乎相同。 They are orthogonal concerns.它们是正交的关注点。

DDD is about domain, so group everything in domain folders . DDD 是关于域的,因此将域文件夹中的所有内容分组 But at the very high level preserve 4 Hexagonal architecture layers .但在非常高的层次上保留 4 个六边形架构层 This could be packages for simplicity or best separate modules with strict mutual dependencies (as in Android Instant Apps).这可以是简单的包,也可以是具有严格相互依赖关系的最佳独立模块(如在 Android Instant Apps 中)。

src/main/java
├── UserInterface
│   └── ... (java files)
├── Application
│   ├── OneUseCase.java
│   ├── AnotherUseCase.java
│   └── YetAnotherUseCase.java
├── Domain
│   ├── SubDomain1
│   │   └── ... (java files)
│   ├── SubDomain2
│   │   └── ... (java files)
│   ├── SubDomain3
│   │   └── ... (java files)
│   └── SubDomain3
│       └── ... (java files)
└── Infrastructure
    ├── database
    │   └── ... (java files)
    ├── logging
    │   └── ... (java files)
    └── httpclient
        └── ... (java files)

Here is a a good article wtih clean explanation.这是一篇很好的文章,解释清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM