洋葱结构与六角形相比 [英] Onion architecture compared to hexagonal

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

问题描述

它们之间是否有任何区别(洋葱|六角形),根据我的理解,它们是相同的,它们专注于应用程序核心的领域,并且应与技术/框架无关.

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.

如果有什么区别?

我也看不出使用一个相对于另一个甚至对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).

另一个区别是六角形对六边形内部结构(应用程序)一无所知.

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).

洋葱的好处是从上面得出的.由于六角形工件很多,因此如果项目很大,则整个项目的构建将花费大量时间.

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.

希望我的解释有所帮助.

Hope my explanation helped.

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

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