.NET中的干净体系结构是什么,我该如何实施?
什么是“干净的建筑”?什么不符合“干净建筑”的资格?
我有兴趣了解.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们参考主要来源,《干净体系结构:工匠的软件结构和设计指南》。这是罗伯特·马丁(Robert Martin)的一些报价:
后来,鲍勃叔叔用相应的四层描述了清洁体系结构的实现,给了它们明确的名称:
对于层之间的通信,我们使用界面和抽象方法及其在外层中的实现。

好的,这是原始的,真正的干净体系结构,如果我们谈论.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)。
关于什么不符合干净体系结构的资格,没有明确的答案,因为不同的开发人员可能对如何应用其原则和准则具有不同的解释和意见。
但是,某些可能不符合干净体系结构的示例包括:
规则,哪个规定源代码依赖性只能指向
向内走向高级政策。在这样的应用中
混凝土层或依赖关系之间的依赖关系
实施而不是抽象存在。
原理,该原则指出,班级或模块只能有一个
更改的理由将是一个不符合资格的例子
干净的体系结构。在这样的应用程序中,类或模块将
有多个责任或担忧,使他们具有挑战性
了解,修改或测试。
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:
Later Uncle Bob describes the implementation of The Clean Architecture with the corresponding four layers, giving them explicit names:
For communication between layers, we use interfaces and abstract methods and their implementation in outer layers.

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).
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:
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.
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.
由于这个问题太广泛,因此我试图高水平回答。
?架构主要是关于管理依赖性的,因为依赖性是代码闻起来像 fragility ,刚性和固定性的主要问题。它通常也称为系统的结构,我可以以与Java,JavaScript甚至C ++应用程序相同的方式构造.NET应用程序。 存储库的概念,用例实现根据语言功能有所不同。
我会说每个架构都打破了将业务价值与技术细节分开的主要规则。这是清洁体系结构的核心 - 使业务规则技术不可知论,以使其易于测试。
因此,每当您拥有需要启动复杂框架,Web服务器或数据库(必须使用DDL和DML初始化的数据库)的系统结构时,您就没有干净的体系结构。
不,这些概念通常与干净的体系结构非常吻合,但是它们是干净的体系结构不需要的概念。例如,您可以将域逻辑作为贫血模型实现,并且仍然符合干净的体系结构。但是我认为使用DDD或至少一种丰富的领域模型将是一个更好的主意。
洋葱架构是 jeffrey palermo 。他还想将业务规则与技术细节解除。杰弗里·巴勒莫(Jeffrey Palermo)说:
,他说:
因此,干净的拱形结构和洋葱架构有很多共同点。这并不是一个大惊喜,因为罗伯特·马丁(Robert C. “ nofollow noreferrer”> 干净的体系结构 博客:
因此,干净的体系结构是对其他建筑的整合,这些建筑与罗伯特·C·马丁(Robert C. Martin)的想法得到了增强。
希望我的答案可以帮助您对不同的术语进行分类。
Since the question is too broad, I try to answer it on a high level.
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.
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.
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.
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:
and he says:
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:
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.