洋葱架构:UI 项目是否应该访问域层?

Onion Architecture: Should UI project have access to Domain Layer?

我在看 Onion Architecture 馅饼,我很困惑...

UI 客户端是否应该有权访问 Domain ModelDomain Services?或者只发送给 Application ServicesCore?

确切地说,我问的是:UI Project 是否应该引用 Domain 项目?

当然,我认为没有充分的理由避免 UI 层来了解和使用领域对象。很明显,如果你这样做,你是在将 UI 层耦合到业务层,但最终,UI 层不会依赖于业务层吗?

另一方面,相反的情况是不可取的,将业务耦合到 UI 层并不是一个好的做法。如果你想让多个 UI 层访问相同的业务怎么办?就是这个意思。

但请记住,没有金锤子。不要寻找有关软件设计的明确规则,而是学习 SOLID 原则,这是值得的。

洋葱架构和类似概念中最重要的概念是保持领域层独立。这是通过将依赖项限制为始终指向您发布的图表内部来实现的。

UI 项目对域项目的引用不违反此规则。

UI 层应该只为每个用例向应用层发送命令。然而,即使有了如此清晰的关注点分离,您可能仍希望在这些命令中包含一些域层对象——想到的最简单的示例是值类型和枚举。

引自 Robert C. Martin's blog 关于依赖关系的引述:

The Dependency Rule

The concentric circles represent different areas of software. In general, the further in you go, the higher level the software becomes. The outer circles are mechanisms. The inner circles are policies.

The overriding rule that makes this architecture work is The Dependency Rule. This rule says that source code dependencies can only point inwards. Nothing in an inner circle can know anything at all about something in an outer circle. In particular, the name of something declared in an outer circle must not be mentioned by the code in the an inner circle. That includes, functions, classes. variables, or any other named software entity.

By the same token, data formats used in an outer circle should not be used by an inner circle, especially if those formats are generate by a framework in an outer circle. We don’t want anything in an outer circle to impact the inner circles.

编辑

在您的评论中,您提到将实体从域层移动到应用程序核心。在onion architecture中,术语'Application core'表示领域模型+领域服务+应用服务。这不是一个单独的图层。