.NET中的干净体系结构是什么,我该如何实施?

发布于 2025-02-06 23:35:44 字数 434 浏览 1 评论 0原文

什么是“干净的建筑”?什么不符合“干净建筑”的资格?

我有兴趣了解.NET中的清洁体系结构以及如何有效地实施它。以下是有关我当前设置以及我想实现的目标的一些细节:

环境:.NET 8 项目类型:ASP.NET核心Web API 当前的体系结构:整体上有一些问题分离

问题:

干净体系结构的核心原则是什么,以及它们如何适用于.NET项目? 我应该如何构建.NET项目以遵循干净的体系结构原则? 在ASP.NET Core Web API中实现干净体系结构的最佳实践是什么? 您可以提供示例或代码段来说明.NET中清洁体系结构的实现吗? 采用干净的建筑时,有哪些共同的挑战和陷阱,我该如何避免它们? 其他上下文:

数据库:SQL Server 依赖项:实体框架核心,MediaTR,AutoMapper 目标:提高应用程序的可维护性,可检验性和可伸缩性 任何见解,资源或示例都将不胜感激!

What is "Clean Architecture" ? What doesn't qualify as "Clean Architecture" ?

I’m interested in learning about Clean Architecture in .NET and how to implement it effectively. Here are some details about my current setup and what I’m looking to achieve:

Environment: .NET 8
Project Type: ASP.NET Core Web API
Current Architecture: Monolithic with some separation of concerns

Questions:

What are the core principles of Clean Architecture, and how do they apply to .NET projects?
How should I structure my .NET project to follow Clean Architecture principles?
What are the best practices for implementing Clean Architecture in an ASP.NET Core Web API?
Can you provide examples or code snippets that illustrate the implementation of Clean Architecture in .NET?
What are the common challenges and pitfalls when adopting Clean Architecture, and how can I avoid them?
Additional Context:

Database: SQL Server
Dependencies: Entity Framework Core, MediatR, AutoMapper
Goals: Improve maintainability, testability, and scalability of my application
Any insights, resources, or examples would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝蛊 2025-02-13 23:35:44

什么是“干净体系结构”?

让我们参考主要来源,《干净体系结构:工匠的软件结构和设计指南》。这是罗伯特·马丁(Robert Martin)的一些报价:

建筑师可以采用单一责任原则共同的闭合原理 分离出出于不同原因而变化的事物,然后<< em>以相同的原因收集那些变化的事物 - 考虑到系统意图的上下文。


因此,我们发现系统分为十至少四个水平层 - 独立于应用程序的业务规则特定于应用程序特定的业务规则 ui 和数据库


后来,鲍勃叔叔用相应的四层描述了清洁体系结构的实现,给了它们明确的名称:

  • 企业业务规则(实体);
  • 申请业务规则(用例);
  • 接口适配器;
  • 框架&amp;司机;

...在这些组件中排列代码,以使它们之间的箭头指向一个方向 - 核心业务。

您应该将其视为依赖性反转的应用
原理
稳定的抽象原理。依赖性箭头被安排为从低级细节指向高级抽象。

对于层之间的通信,我们使用界面和抽象方法及其在外层中的实现。

好的,这是原始的,真正的干净体系结构,如果我们谈论.NET,那么我们可以看到,即使他们使用相同的参考文献,Microsoft文档也具有不同的观点。在这里是 https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-aps-azure/common-web-application-architection-architection-architures-actitures-yarkitures-uncorecation-uncartecture-application-architures-ucconectures#clean-architecture 。但是该文档使用该术语“干净的体系结构”作为模式数量的通用描述,例如“洋葱架构”,“六边形体系结构”,“端口和适应器”,说这是同一件事,完全贬低了罗伯特马丁斯的工作。

因此,如果您遵循由叔叔鲍勃bob < /a>,作为主要来源,并使用 Microsoft指令对于零件,当某些内容不清楚时,我相信可以将其视为干净的体系结构(*in .net)。

什么不符合“干净体系结构”?

关于什么不符合干净体系结构的资格,没有明确的答案,因为不同的开发人员可能对如何应用其原则和准则具有不同的解释和意见。

但是,某些可能不符合干净体系结构的示例包括:

  • 单片应用程序在业务逻辑,演示,数据访问和外部依赖关系之间缺乏分离。这种类型的应用程序将与特定的技术或框架紧密相关,从而难以测试,更改或重复使用。
  • 另一个示例是一个分层应用程序,违反了依赖关系
    规则,哪个规定源代码依赖性只能指向
    向内走向高级政策
    。在这样的应用中
    混凝土层或依赖关系之间的依赖关系
    实施而不是抽象存在。
  • 最后,一个不遵循单一责任的应用程序
    原理,该原则指出,班级或模块只能有一个
    更改的理由
    将是一个不符合资格的例子
    干净的体系结构。在这样的应用程序中,类或模块将
    有多个责任或担忧,使他们具有挑战性
    了解,修改或测试。

What is "Clean Architecture" ?

Let's refer to the primary source, the book "Clean Architecture: A Craftsman's Guide to Software Structure and Design". Here are some quotes from Robert Martin:

The architect can employ the Single Responsibility Principle and the Common Closure Principle to separate those things that change for different reasons, and to collect those things that change for the same reasons — given the context of the intent of the system.

Thus we find the system divided into decoupled at least four horizontal layers - the Application-independent business rules, Application-specific business rules, UI, and the Database.

Later Uncle Bob describes the implementation of The Clean Architecture with the corresponding four layers, giving them explicit names:

  • Enterprise Business Rules (Entities);
  • Application Business Rules (Use Cases);
  • Interface Adapters;
  • Frameworks & Drivers;

...arrange the code in those components such that the arrows between them point in one direction—toward the core business.

You should recognize this as an application of the Dependency Inversion
Principle
and the Stable Abstractions Principle. Dependency arrows are arranged to point from lower-level details to higher-level abstractions.

For communication between layers, we use interfaces and abstract methods and their implementation in outer layers.
enter image description here

Ok it was the original, true Clean Architecture, if we talk about .NET, then we can see, that microsoft documentation has a different point of view on it, even though they use the same reference. Here it is https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures#clean-architecture. But that documentation use that term "Clean Architecture" as the generic description of number of patterns, such as "Onion Architecture", "Hexagonal Architecture", "Ports-and-Adapters", saying that it is the same thing, completely devaluing Robert Martins's work.

So if you follow instructions provided by Uncle Bob, as the primary source and use Microsoft instructions for parts, when something is not clear, I believe that could be considered as the Clean Architecture (*in .NET).

What doesn't qualify as "Clean Architecture" ?

There is no clear-cut answer on what doesn't qualify as Clean Architecture since different developers may have varying interpretations and opinions on how to apply its principles and guidelines.

However, some examples of what may not qualify as Clean Architecture include:

  • A monolithic application that lacks separation between business logic, presentation, data access, and external dependencies. This type of application would be tightly linked to particular technologies or frameworks, making it difficult to test, change, or reuse.
  • Another example is a layered application that violates the Dependency
    Rule, which states that source code dependencies should only point
    inward toward higher-level policies
    . In such an application, circular
    dependencies between layers or dependencies on concrete
    implementations rather than abstractions would be present.
  • Finally, an application that doesn't follow the Single Responsibility
    Principle, which states that a class or module should only have one
    reason to change
    , would be an example of what doesn't qualify as
    Clean Architecture. In such an application, classes or modules would
    have multiple responsibilities or concerns, making them challenging
    to understand, modify, or test.
给妤﹃绝世温柔 2025-02-13 23:35:44

由于这个问题太广泛,因此我试图高水平回答。

.net中的“干净架构”是什么?

?架构主要是关于管理依赖性的,因为依赖性是代码闻起来像 fragility 刚性固定性的主要问题。它通常也称为系统的结构,我可以以与Java,JavaScript甚至C ++应用程序相同的方式构造.NET应用程序。 存储库的概念用例实现根据语言功能有所不同。

什么不符合“干净体系结构”?

我会说每个架构都打破了将业务价值与技术细节分开的主要规则。这是清洁体系结构的核心 - 使业务规则技术不可知论,以使其易于测试。

因此,每当您拥有需要启动复杂框架,Web服务器或数据库(必须使用DDL和DML初始化的数据库)的系统结构时,您就没有干净的体系结构。

CQRS是“干净体系结构”的强制性DDD?

不,这些概念通常与干净的体系结构非常吻合,但是它们是干净的体系结构不需要的概念。例如,您可以将域逻辑作为贫血模型实现,并且仍然符合干净的体系结构。但是我认为使用DDD或至少一种丰富的领域模型将是一个更好的主意。

什么是洋葱架构?

洋葱架构是 jeffrey palermo 。他还想将业务规则与技术细节解除。杰弗里·巴勒莫(Jeffrey Palermo)说:

六边形体系结构和洋葱架构共享以下前提:外部化基础架构和编写适配器代码,以使基础架构不会紧密耦合。

,他说:

数据库不是中心。它是外部的。

因此,干净的拱形结构和洋葱架构有很多共同点。这并不是一个大惊喜,因为罗伯特·马丁(Robert C. “ nofollow noreferrer”> 干净的体系结构 博客:

在过去的几年中,我们看到了有关系统架构的一系列想法。这些包括:

  • 六边形体系结构(又名端口和适配器)由Alistair Cockburn,并由Steve Freeman采用,而Nat Pryce在他们精彩的书籍成长中成长为面向对象的软件
  • Jeffrey Palermo的洋葱架构
  • 去年从我的博客中尖叫架构
  • dci来自詹姆斯·科普利安(James Coplien)和trygve reenskaug。
  • bce撰写的伊瓦尔·雅各布森(IVAR Jacobson

尽管这些体系结构在细节上都有所不同,但它们非常相似。

因此,干净的体系结构是对其他建筑的整合,这些建筑与罗伯特·C·马丁(Robert C. Martin)的想法得到了增强。

希望我的答案可以帮助您对不同的术语进行分类。

Since the question is too broad, I try to answer it on a high level.

What is "Clean Architecture" in .NET ?

In short: "The same as in every other language". Architecture is mainly about managing dependencies, because dependencies are the main problem when it comes to code smells like fragility, rigidity and immobility. It is also often called the structure of the system and I can structure .NET application the same way as Java, JavaScript or even C++ applications. The concepts of repositories, use cases (interactors), entities and so on stay the same, even though their implementation differ based on the language features.

What doesn't qualify as "Clean Architecture" ?

I would say each architecture that breaks the main rule of separating the business value from technical details. That's the core of the clean architecture - to make the business rules technology agnostic with the goal to make them easy to test.

So whenever you have a system structure that needs to boot up a complex framework, a web server or a database (that must be initialized with ddl and dml) just to test your business rules, you don't have a clean architecture.

Are CQRS, DDD mandatory for "Clean Architecture" ?

No, these concept usually fit very well with the clean architecture, but they are concepts that the clean architecture does not require. E.g. you can implement your domain logic as an anemic model and still be clean architecture compliant. But I think it would be a better idea to use DDD or at least a kind of rich domain model.

What is onion architecture ?

The onion architecture is an architecture that was introduced by Jeffrey Palermo. He also wants to decouple the business rules from the technology details. Jeffrey Palermo says:

Hexagonal architecture and Onion Architecture share the following premise: Externalize infrastructure and write adapter code so that the infrastructure does not become tightly coupled.

and he says:

The database is not the center. It is external.

Thus the clean archtitecture and the onion architecture have a lot in common. That is not a big suprise, because Robert C. Martin says in his The Clean Architecture blog:

Over the last several years we’ve seen a whole range of ideas regarding the architecture of systems. These include:

  • Hexagonal Architecture (a.k.a. Ports and Adapters) by Alistair Cockburn and adopted by Steve Freeman, and Nat Pryce in their wonderful book Growing Object Oriented Software
  • Onion Architecture by Jeffrey Palermo
  • Screaming Architecture from a blog of mine last year
  • DCI from James Coplien, and Trygve Reenskaug.
  • BCE by Ivar Jacobson from his book Object Oriented Software Engineering: A Use-Case Driven Approach

Though these architectures all vary somewhat in their details, they are very similar.

Thus the clean architecture is a consolidation of other architectures that is enhanced with ideas from Robert C. Martin.

I hope my answer helps you to classify the different terms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文