洋葱结构与六边形结构的比较 [英] Onion architecture compared to hexagonal

查看:123
本文介绍了洋葱结构与六边形结构的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们之间有什么区别吗(洋葱 | 六边形),根据我的理解,它们是一样的,它们专注于作为应用程序核心的领域,应该与技术/框架无关.

Is there any difference between them (onion | hexagonal), from my understanding they are just the same, they focus upon the domain which is at the core of the application and should be technology / framework agnostic.

如果有的话,它们之间有什么区别?

What are the differences between them if any ?

此外,我认为使用一个而不是另一个甚至对抗 N 层架构没有真正的优势,如果做得不好,仅仅遵循其中任何一个都不会有任何区别

Also I see no real advantage on using one over the other or even against an N-layered architecture, if done bad just following any of them won't make any difference

使用其中一种的好处是什么,为什么要使用它?什么时候用?

What are the benefits of using one over the other and why you would use it ? when to use it?

谢谢

推荐答案

如果有的话,它们之间有什么区别?

What are the differences between them if any?

洋葱:有层,依赖项总是向内,即一个层可以使用它内部的任何层.内层是领域模型,外层是基础设施,但层数可能会有所不同.

Onion: There are layers, with the dependencies always pointing inwards, i.e., a layer can use any of the layers inside it. The inner layer is Domain Model and the outer is infrastructure, but the number of layers between may vary.

六角形(它是原始名称端口和适配器"的替代名称):没有层.您拥有应用程序、端口和适配器.端口属于应用程序,它们是应用程序的 API/SPI.适配器在应用程序之外,每个适配器都依赖于应用程序的一个端口.

Hexagonal (it's an alternative name for the original name "Ports & Adapters"): There are no layers. You have the application, the ports, and the adapters. The ports belong to the application, they are the API/SPI of the application. Adapters are outside the application, and each one depends on a port of the application.

有些人的困惑是,在实现六边形架构时,大多数人并没有将每个适配器物理地放在一个工件中,而是将所有适配器放在一个工件中(如基础设施层).而且它们还依赖于整个应用程序上的适配器,而不仅仅是它们使用的端口.所以它实际上是一个洋葱.

The confusion some people have is that when implementing the hexagonal architecture, most of the people don't physically put every adapter in an artifact, but they put all together into one artifact (like the infrastructure layer). And also they make depend on the adapters on the whole app, not just the port they use. So it would be an Onion in fact.

实现六边形权利应该将适配器彼此分开,每个适配器应该只依赖于它使用/实现的端口(取决于端口是驱动还是驱动).

Implementing hexagonal right should separate adapters from each other, and every adapter should depend just on the port it uses/implements (depending on whether the port is a driver or driven).

另一个区别是 Hexagonal 没有说明六边形内部的结构(应用程序).

Another difference is that Hexagonal says nothing about the structure of the inside of the hexagon (the application).

使用一个比另一个有什么好处?

What are the benefits of using one over the other?

六边形的好处是它更加模块化,您可以清楚地分离组件,防止它们之间的代码泄漏.另一方面,洋葱在这个意义上更危险,因为您可以直接从 UI 访问例如数据库(它们都属于同一层).

The benefit of hexagonal is that it is more modular, you have a clear separation of components, preventing the leaking of code between them. Onion, on the other hand, is more dangerous in that sense, as you can access for example the database directly from the UI (they both belong to the same layer).

Onion 的好处源于以上.由于六边形有很多工件,如果项目很大,整个项目的构建应该花费很多时间.

The benefit of Onion is derived from the above. As hexagonal have a lot of artifacts, if the project is big, the build of the whole project should take a lot of time.

为什么要使用它?什么时候用?

Why you would use it? When to use it?

使用它们中的任何一个的要点是您专注于您试图解决的实际问题,而不使用任何技术或框架.该应用程序与技术无关,并且很容易从一个框架迁移到另一个框架.因此,它们都被称为干净"架构.您的应用程序核心没有框架代码、注释等.

The point of using either of them is that you focus on the real problem you are trying to solve, without using any technology or framework. The application is technology agnostic, and it is easy to migrate from a framework to another. Both of them are called "clean" architectures because of that. You have your application core free of framework code, annotations, etc.

那么……为什么要使用它们?

So... Why use them?

因为您提高了可维护性、可测试性,并且您拥有干净的代码.

Because you improve maintainability, testability, and you have clean code.

何时使用它们?

我应该说什么时候不使用它们.如果您正在开发的应用并不复杂,例如,它只是一个 CRUD,则可能不值得使用它们.

I should rather say when not to use them. If the app you are developing is not complex, for example, it's just a CRUD, maybe it doesn't deserve to use them.

就个人而言,我更喜欢端口和适配器".

Personally, I like "Ports and Adapters" over the others.

希望我的解释有帮助.

这篇关于洋葱结构与六边形结构的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆