Clean Architecture

Robert C. Martin

a.k.a. Uncle Bob

Business Rules are the most important part of an application

  • Stuff we want to keep
  • Stuff we want to test
  • Stuff we want to hide

So, everything else is plugins

  • Database
  • UI
  • Web
  • Services

By coincidence (or not)

  • DB is hard to test
  • UI is hard to test
  • Web is hard to test
  • External services are hard to test

The Clean Architecture

Inversion of Control

"Don't call us, we'll call you"

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.

  • Abstractions should not depend on details. Details should depend on abstractions.

Entities

  • "Entities encapsulate Enterprise wide business rules"
  • They are the least likely to change when something external changes

Use Cases

  • It encapsulates and implements all of the use cases of the system
  • Orchestrate the flow of data to and from the entities
  • What your application does

Interface Adapters

"Converts data from the format most convenient for the use cases and entities, to the format most convenient for some external agency"

  • SQL queries
  • MVC
  • ORM

External world

  • Details
  • MySQL
  • Web Server

Cool consequences

  • It's fairly easy to tell what your application does
  • Test your business rules without external dependencies
  • Postpone decision making
➜ tree -L 1
.
├── assets
├── channels
├── controllers
├── helpers
├── jobs
├── mailers
├── models
└── views

8 directories, 0 files
➜ tree -L 2
.
├── assets
│   ├── config
│   ├── images
│   ├── javascripts
│   └── stylesheets
├── channels
│   └── application_cable
├── controllers
│   ├── application_controller.rb
│   ├── concerns
│   ├── todo_items_controller.rb
│   └── todo_lists_controller.rb
├── helpers
│   ├── application_helper.rb
│   ├── todo_items_helper.rb
│   └── todo_lists_helper.rb
├── jobs
│   └── application_job.rb
├── mailers
│   └── application_mailer.rb
├── models
│   ├── application_record.rb
│   ├── concerns
│   ├── todo_item.rb
│   └── todo_list.rb
└── views
    ├── layouts
    ├── todo_items
    └── todo_lists

18 directories, 11 files
➜ tree -L 3
.
├── assets
│   ├── config
│   │   └── manifest.js
│   ├── images
│   ├── javascripts
│   │   ├── application.js
│   │   ├── cable.js
│   │   ├── channels
│   │   ├── todo_items.coffee
│   │   └── todo_lists.coffee
│   └── stylesheets
│       ├── application.scss
│       ├── scaffolds.scss
│       ├── todo_items.scss
│       └── todo_lists.scss
├── channels
│   └── application_cable
│       ├── channel.rb
│       └── connection.rb
├── controllers
│   ├── application_controller.rb
│   ├── concerns
│   ├── todo_items_controller.rb
│   └── todo_lists_controller.rb
├── helpers
│   ├── application_helper.rb
│   ├── todo_items_helper.rb
│   └── todo_lists_helper.rb
├── jobs
│   └── application_job.rb
├── mailers
│   └── application_mailer.rb
├── models
│   ├── application_record.rb
│   ├── concerns
│   ├── todo_item.rb
│   └── todo_list.rb
└── views
    ├── layouts
    │   ├── application.html.erb
    │   ├── mailer.html.erb
    │   └── mailer.text.erb
    ├── todo_items
    │   ├── _form.html.erb
    │   └── _todo_item.html.erb
    └── todo_lists
        ├── _form.html.erb
        ├── _todo_list.json.jbuilder
        ├── edit.html.erb
        ├── index.html.erb
        ├── index.json.jbuilder
        ├── new.html.erb
        ├── show.html.erb
        └── show.json.jbuilder

19 directories, 35 files

...a simple example

Final Considerations

  • It's not something completely new and innovative:
    • Code for an interface not an implementation
    • Clear separation of concerns
  • There's an overhead on transforming and adapting data
  • However you get a more maintainable and testable application

The End

  • https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
  • https://www.youtube.com/watch?v=Nsjsiz2A9mg
  • https://martinfowler.com/bliki/InversionOfControl.html

Clean Architecture

By Mateus Chagas Sousa