天天看點

Clean_Achitecture_翻譯

萌新翻譯,勿噴

臨時工出品,一切概不負責

原文戳這裡

Clean_Achitecture_翻譯

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

  • 六角結構 (出現在《Growing Obejct Oriented Software》)檢視
  • 洋蔥結構檢視
  • 流結構檢視
  • DCI結構(data資料,context場景,interaction互動)檢視
  • BCE結構(Boundary-Control-Entity )檢視

Though these architectures all vary somewhat in their details, they are very similar. They all have the same objective, which is the separation of concerns. They all achieve this separation by dividing the software into layers. Each has at least one layer for business rules, and another for interfaces.

這些結構除了某些細節上有所不同,都非常的相似。為了達成關系的分離,他們都采用了軟體分層。每個都隻有一個業務規則層,以及一些其它的接口。

Each of these architectures produce systems that are:

每個結構系統都是:

Independent of Frameworks. The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints.

Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.

Independent of UI. The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules.

Independent of Database. You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database.

Independent of any external agency. In fact your business rules simply don’t know anything at all about the outside world.

The diagram at the top of this article is an attempt at integrating all these architectures into a single actionable idea.

  • 無架構依賴。這些結構都不依賴與特定的庫。這允許你把架構當工具用,而不是在限制限制下填充你的系統
  • 可測性。這些業務規則能夠在沒有界面,資料庫,web伺服器或者任何其它的外部元素的情況下被測試。
  • 無界面依賴。界面可以很容易的在不改動系統其它部分的情況進行改動。舉個例子,在沒有改動業務邏輯的情況下,一個web的界面可能被替換成控制台的界面。
  • CounchDB(面向文檔的資料庫).業務邏輯沒有與資料庫綁定
  • 無外部代理依賴。事實上,你的業務邏輯非常的簡單,完全不知道外部的情況。

文章頂部的圖檔嘗試內建這些結構為一個單獨可行的想法。

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.

同樣的,外部圓使用的資料格式也不能被内部的圓使用,尤其是外部圓通過架構産生來某種資料格式。我們不想外部圓的任何東西能夠影線到内部的圓。

Entities

Entities encapsulate Enterprise wide business rules. An entity can be an object with methods, or it can be a set of data structures and functions. It doesn’t matter so long as the entities could be used by many different applications in the enterprise.

實體封裝基本的企業業務邏輯。一個實體可以是一些方法,也可以是一組資料結構,或者一些功能。隻要這些實體能夠被企業中的任何一個應用使用可以了。

If you don’t have an enterprise, and are just writing a single application, then these entities are the business objects of the application. They encapsulate the most general and high-level rules. They are the least likely to change when something external changes. For example, you would not expect these objects to be affected by a change to page navigation, or security. No operational change to any particular application should affect the entity layer.

假如你沒有企業,僅僅是寫一個單應用程式。這些實體就是應用業務對象。它們是對一些當外部産生變化時,裡面最不可能産生變化的最通用,最進階規則的封裝。舉個例子,你可能不希望這些對象被頁面的導航欄,安全性等影線。沒有操作能夠影響到實體層

Use Cases

The software in this layer contains application specific business rules. It encapsulates and implements all of the use cases of the system. These use cases orchestrate the flow of data to and from the entities, and direct those entities to use their enterprise wide business rules to achieve the goals of the use case.

軟體的這一層包含軟體的特定業務邏輯。它封裝和實作一些系統的用例。這些用例精心組織資料進出實體層,并且指揮實體層去實作用例。

We do not expect changes in this layer to affect the entities. We also do not expect this layer to be affected by changes to externalities such as the database, the UI, or any of the common frameworks. This layer is isolated from such concerns.

**我們不希望用例層的改變影響到實體層。我們同樣不希望這一層被外部的像資料庫,UI等影響。

這一次層是獨立的。**

We do, however, expect that changes to the operation of the application will affect the use-cases and therefore the software in this layer. If the details of a use-case change, then some code in this layer will certainly be affected.

我們認為,無論如何,應用的操作改變将影響用例,最終會影響到軟體的這一層。将如用例的細節發生了改變,這一層的一些代碼是會有影線的。

Interface Adapters

The software in this layer is a set of adapters that convert data from the format most convenient for the use cases and entities, to the format most convenient for some external agency such as the Database or the Web. It is this layer, for example, that will wholly contain the MVC architecture of a GUI. The Presenters, Views, and Controllers all belong in here. The models are likely just data structures that are passed from the controllers to the use cases, and then back from the use cases to the presenters and views.

在軟體的這一層是一些擴充卡,它們将最适合用例和實體的資料格式轉換為最适合外部代理(像資料庫或者Web)最适用的格式。這一層将完成的包含GUI的MVC結構。Preservertes,Views,Controllers 都屬于這裡。Models更像是從Controllers傳遞到Use Cases,再從User Case傳回到Presenters和Views 的資料結構

Similarly, data is converted, in this layer, from the form most convenient for entities and use cases, into the form most convenient for whatever persistence framework is being used. i.e. The Database. No code inward of this circle should know anything at all about the database. If the database is a SQL database, then all the SQL should be restricted to this layer, and in particular to the parts of this layer that have to do with the database.

Also in this layer is any other adapter necessary to convert data from some external form, such as an external service, to the internal form used by the use cases and entities.

Frameworks and Drivers.

The outermost layer is generally composed of frameworks and tools such as the Database, the Web Framework, etc. Generally you don’t write much code in this layer other than glue code that communicates to the next circle inwards.

This layer is where all the details go. The Web is a detail. The database is a detail. We keep these things on the outside where they can do little harm.

Only Four Circles?

No, the circles are schematic. You may find that you need more than just these four. There’s no rule that says you must always have just these four. However, The Dependency Rule always applies. Source code dependencies always point inwards. As you move inwards the level of abstraction increases. The outermost circle is low level concrete detail. As you move inwards the software grows more abstract, and encapsulates higher level policies. The inner most circle is the most general.

Crossing boundaries.(跨邊界)

At the lower right of the diagram is an example of how we cross the circle boundaries. It shows the Controllers and Presenters communicating with the Use Cases in the next layer. Note the flow of control. It begins in the controller, moves through the use case, and then winds up executing in the presenter. Note also the source code dependencies. Each one of them points inwards towards the use cases.

在圖的右下是一個怎麼跨過圓的邊界的例子。它展示了Controllers 和 Presenter與 處于下一層的用例層進行互動。注意控制流。它開始于Controller,移動到用例層,之後收尾運作在Presenter.也需要注意源碼的依賴。每一個點展示洩漏了的用例層的内部情況。

We usually resolve this apparent contradiction by using the Dependency Inversion Principle. In a language like Java, for example, we would arrange interfaces and inheritance relationships such that the source code dependencies oppose the flow of control at just the right points across the boundary.

通常情況下,我們解決這種沖突使用依賴倒轉原則。在java語言中,我們可以通過接口和繼承實作源代碼的依賴反轉控制流,僅僅正确的點通過邊界。

For example, consider that the use case needs to call the presenter. However, this call must not be direct because that would violate The Dependency Rule: No name in an outer circle can be mentioned by an inner circle. So we have the use case call an interface (Shown here as Use Case Output Port) in the inner circle, and have the presenter in the outer circle implement it.

舉個例子:考慮一種情況,用例需要調用Presenter.但是這種調用不能直接調用,因為它會違反沒有外層能夠影響内層的原則。是以,用例層可以調用接口(表現在這裡就是用例輸出口),Presenter和外層實作它。

The same technique is used to cross all the boundaries in the architectures. We take advantage of dynamic polymorphism to create source code dependencies that oppose the flow of control so that we can conform to The Dependency Rule no matter what direction the flow of control is going in.

這類似的計數被使用在這個結構的所有結構。我們利用動态的多态性去創造源代碼依賴來反轉控制流,這樣,我們可以服從依賴原則,而不是關系控制流的去向。

What data crosses the boundaries.

什麼樣的資料來穿過邊界。

Typically the data that crosses the boundaries is simple data structures. You can use basic structs or simple Data Transfer objects if you like. Or the data can simply be arguments in function calls. Or you can pack it into a hashmap, or construct it into an object. The important thing is that isolated, simple, data structures are passed across the boundaries. We don’t want to cheat and pass Entities or Database rows. We don’t want the data structures to have any kind of dependency that violates The Dependency Rule.

典型的,通過邊界的是簡單的資料結構體。你能使用基本的結構或者簡單的資料傳輸對象。或者,資料可以簡單是函數調用的參數。又或者你能夠包裝它到一個哈希Map或對象。最重要是這些都是獨立的,簡單的,資料體被傳遞通過資料邊界。我們不想欺騙或者傳遞實體或者資料庫行。我們不想資料的結構有任何的依賴違法依賴原則。

For example, many database frameworks return a convenient data format in response to a query. We might call this a RowStructure. We don’t want to pass that row structure inwards across a boundary. That would violate The Dependency Rule because it would force an inner circle to know something about an outer circle.

舉個例子,很多的資料庫框解會傳回一個合适的資料格式來響應查詢。我們可以叫它RowStructure.我們不想傳遞這個RowStructre到内部。這違法了原則,這将會強迫内層知道外層的細節。

So when we pass data across a boundary, it is always in the form that is most convenient for the inner circle.

是以,當我們跨邊界傳遞資料時,它都是最适合内層圓的格式。

Conclusion (結論)

Conforming to these simple rules is not hard, and will save you a lot of headaches going forward. By separating the software into layers, and conforming to The Dependency Rule, you will create a system that is intrinsically testable, with all the benefits that implies. When any of the external parts of the system become obsolete, like the database, or the web framework, you can replace those obsolete elements with a minimum of fuss.

服從這些簡單的規則不是很困難,但是将減少疊代的頭痛。通過軟體的分層,服從依賴原則,你将建立一個真正可測的系統。系統的外層部分像資料庫或者Web架構,變的廢棄,你能夠已最小的代價簡單的替換這些廢棄的元素。

Robert Martin (Uncle Bob) is a Master Craftsman. He’s an award-winning author, renowned speaker, and has been an über software geek since 1970.

related posts

Loose Coupling in Go lang Javier Saldana

Mistaking Encapsulation for Abstraction Kevin Buchanan

Thinking Differently About the Single Responsibility Principle Cory Foy

NO DB Uncle Bob

Service Oriented Agony Uncle Bob

What Is Assumed When Deferring Database Decisions? Patrick Gombert

Concurrency Control Strategies for Secret Agents Kevin Buchanan

Decomposing Asynchronous Workers in Ruby Kevin Buchanan

Organizing Your Clojure Environment and Logs with Leiningen Kevin Buchanan

Take Pride in Your Legacy (Code) Ginny Hendry

more posts by this author

Future Proof

Agile is not now, nor was it ever, Waterfall.

VW

WATS Line 54

A Little Structure

Make the Magic go away.

The Little Singleton

The First Micro-service Architecture

Language Layers

Does organization matter?