Slide 1

Slide 1 text

CLEAN ARCHITECTURE ON RAILS

Slide 2

Slide 2 text

CHAPTER I What is Clean Architecture?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

the PROBLEM

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

your top level structure is screaming the web FRAMEWORK

Slide 7

Slide 7 text

The web is a delivery mechanism

Slide 8

Slide 8 text

the web is a COMMODITY

Slide 9

Slide 9 text

yet it DOMINATES your code

Slide 10

Slide 10 text

image you’ve just joined a PROJECT

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

what makes a GOOD architecture?

Slide 14

Slide 14 text

it is not about frameworks and tools

Slide 15

Slide 15 text

Fabiano Beselga CTO magnetis.com.br WRITER medium.com/@fbzga TWITTER @fbzga

Slide 16

Slide 16 text

bit.ly/clean-rails

Slide 17

Slide 17 text

IVAR JACOBSON

Slide 18

Slide 18 text

business rules INTERACTORS (aka use cases)

Slide 19

Slide 19 text

domain model ENTITIES

Slide 20

Slide 20 text

communication interfaces BOUNDARIES

Slide 21

Slide 21 text

diagrams: Robert C Martin

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

What about MVC?

Slide 28

Slide 28 text

MVC as a Web architecture

Slide 29

Slide 29 text

this is NOT WHAT I MEANT

Slide 30

Slide 30 text

WEB FRAMEWORK APP

Slide 31

Slide 31 text

CHAPTER II HOW to use it with Rails?

Slide 32

Slide 32 text

BRAAVOS

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

WE ARE OPENING A BANK! WE NEED TO HIRE A GREAT DEVELOPER!

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

WHAT ABOUT THIS ONE?

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

HIRED!

Slide 41

Slide 41 text

first day at WORK

Slide 42

Slide 42 text

THE SYSTEM MUST TRANSFER MONEY BETWEEN THE FAMILIES ACCOUNTS

Slide 43

Slide 43 text

OK! ANY EXCEPTION COURSES?

Slide 44

Slide 44 text

WHEN THERE ISN’T ENOUGH MONEY TO TRANSFER.

Slide 45

Slide 45 text

that’s easy, I can do it in 7 STEPS!

Slide 46

Slide 46 text

GREAT! YOU HAVE 10 MINUTES!

Slide 47

Slide 47 text

challenge accepted!

Slide 48

Slide 48 text

1 create the PROJECT

Slide 49

Slide 49 text

rails new braavos

Slide 50

Slide 50 text

mkdir core

Slide 51

Slide 51 text

module Core end core.rb

Slide 52

Slide 52 text

2 define BOUNDARIES

Slide 53

Slide 53 text

README Driven Development

Slide 54

Slide 54 text

Usage Transfer money Core.transfer_money(amount: 10_000, source_account_id: 1, destination_account_id: 2) README => true # when transfer successful => false # when not enough balance

Slide 55

Slide 55 text

3 define behaviour Driven Development

Slide 56

Slide 56 text

context 'enough money to transfer' do it 'responds true' do expect(transfer).to be true end it 'debits the source account' do expect{ transfer }.to change{ source_account_balance }.by(- amount) end it 'credits the destination account' do expect{ transfer }.to change{ destination_account_balance }.by(amount) end end PRIMARY course

Slide 57

Slide 57 text

context 'not enough money to transfer' do it 'cancels the transfer and responds false' do expect(transfer).to be false end it 'does not change the accounts balance' do expect{ transfer }.to_not change{ source_account_balance } expect{ transfer }.to_not change{ destination_account_balance } end end EXCEPTION COURSE

Slide 58

Slide 58 text

FFF

Slide 59

Slide 59 text

define DATA MODEL 4

Slide 60

Slide 60 text

CAN YOU TELL ME A LITTLE MORE ABOUT THE DOMAIN?

Slide 61

Slide 61 text

EACH FAMILY HAS AN ACCOUNT. EACH ACCOUNT HAS A HISTORY OF TRADES MADE.

Slide 62

Slide 62 text

rails g model account name rails g model trade account:references amount:decimal date:date

Slide 63

Slide 63 text

trades debit: -1000 credit: +1000

Slide 64

Slide 64 text

the ENTRY POINT 5

Slide 65

Slide 65 text

module Core class << self delegate :transfer_money, to: TransferMoney end end core.rb

Slide 66

Slide 66 text

the USE CASE 6

Slide 67

Slide 67 text

module Core class TransferMoney def self.transfer_money(source_account_id:, destination_account_id:, amount:) balance = Core.get_balance(account_id: source_account_id) return false if balance < amount ActiveRecord::Base.transaction do Trade.create!(account_id: source_account_id, amount: -amount) Trade.create!(account_id: destination_account_id, amount: amount) end end end end core/transfer_money.rb

Slide 68

Slide 68 text

Slide 69

Slide 69 text

REFACTOR!

Slide 70

Slide 70 text

module Core class << self delegate :transfer_money, to: TransferMoney end end core.rb before

Slide 71

Slide 71 text

CAZE DSL to elegantly declare use cases

Slide 72

Slide 72 text

Gemfile gem 'caze'

Slide 73

Slide 73 text

module Core include Caze has_use_case :transfer_money, TransferMoney end core.rb AFTER

Slide 74

Slide 74 text

module Core include Caze has_use_case :transfer_money, TransferMoney, transactional: true end core.rb AFTER

Slide 75

Slide 75 text

g github.com/magne/s/caze

Slide 76

Slide 76 text

BREAKING NEWS! 
 WE HAVE MONEY TO HIRE AGAIN!

Slide 77

Slide 77 text

WHAT ABOUT THIS SNOW?

Slide 78

Slide 78 text

WHAT?

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

Usage Transfer money Core.transfer_money(amount: 10_000, source_account_id: 1, destination_account_id: 2) README => true # when transfer successful => false # when not enough balance

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

expose it to the web 7

Slide 84

Slide 84 text

let’s call it on a CONTROLLER

Slide 85

Slide 85 text

class TransferMoneyController < ApplicationController before_action :load_balance def create if Core.transfer_money(source_account_id: source_account_id, destination_account_id: destination_account_id, amount: amount) redirect_to new_transfer_money_path else flash[:error] = 'Not enough money on the source account' render :new end end private def load_balance @balance = Core.get_balance(account_id: current_account_id) end end

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

bit.ly/braavos

Slide 88

Slide 88 text

what just happened?

Slide 89

Slide 89 text

Core represents WHAT THE SYSTEM Does CHANGES

Slide 90

Slide 90 text

core/ get_balance.rb transfer_money.rb BEHAVIOUR CHANGES

Slide 91

Slide 91 text

the models represent WHAT THE SYSTEM IS STABLE

Slide 92

Slide 92 text

app/ models/ account.rb trade.rb DATA STABLE

Slide 93

Slide 93 text

a key, longstanding hallmark of a good program is that it separates what is stable from what changes in the interest of good maintenance. - Trygve Reenskaug

Slide 94

Slide 94 text

Models Core Rails delivery mechanism behaviour MODULE data #1

Slide 95

Slide 95 text

the ALTERNATIVES

Slide 96

Slide 96 text

Models Core Rails delivery mechanism behaviour RAILS ENGINE #2

Slide 97

Slide 97 text

The database is a detail

Slide 98

Slide 98 text

Isolate it!

Slide 99

Slide 99 text

Models Core Rails delivery mechanism behaviour gem rails engine data API #3

Slide 100

Slide 100 text

what about real life?

Slide 101

Slide 101 text

magnetis.com.br

Slide 102

Slide 102 text

~ 4 years old codebase

Slide 103

Slide 103 text

experimenting with clean architecture ~ 2 years

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

the first seeds magnetis core

Slide 106

Slide 106 text

a module for each DOMAIN CONCEPT

Slide 107

Slide 107 text

we have a module for investments recommendation Advisor

Slide 108

Slide 108 text

Advisor.recommend_investments(params)

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

we have a module for accounting Accountant

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

magnetis/ domain/ accountant/ advisor/

Slide 113

Slide 113 text

BOUNDED CONTEXTs Advisor Accountant Order Manager Account Manager Trader Pricing Backtesting

Slide 114

Slide 114 text

why not MICROSERVICES?

Slide 115

Slide 115 text

we also use the Rails WAY

Slide 116

Slide 116 text

FINAL CHAPTER when to use it?

Slide 117

Slide 117 text

YOUR PRODUCT

Slide 118

Slide 118 text

core business your product rails way clean architecture

Slide 119

Slide 119 text

Eric Evans Don’t try yo apply DDD to everything. Draw a context map and decide on where you will make a push for DDD and where you will not .

Slide 120

Slide 120 text

THE distance from the view

Slide 121

Slide 121 text

the domain logic is close to the view Rails way

Slide 122

Slide 122 text

the domain logic is far from the view Clean architecture

Slide 123

Slide 123 text

TIMING

Slide 124

Slide 124 text

image: Kent Beck

Slide 125

Slide 125 text

Lessons learned

Slide 126

Slide 126 text

no good

Slide 127

Slide 127 text

Over-engineering when the domain logic is close to the view

Slide 128

Slide 128 text

getting out of the rails conventions may be hard

Slide 129

Slide 129 text

the cool STUFF

Slide 130

Slide 130 text

defer decisions until the last responsible moment

Slide 131

Slide 131 text

it SCREAMS BEHAVIOR

Slide 132

Slide 132 text

decoupled business rules

Slide 133

Slide 133 text

tests RUNNING FASTER!

Slide 134

Slide 134 text

what makes a GOOD architecture?

Slide 135

Slide 135 text

CLEAN ARCHITECTURE HEXAGONAL ARCHITECTURE DOMAIN DRIVEN DESIGN DATA CONTEXT INTERACTION TRAILBLAZER

Slide 136

Slide 136 text

all these architectures have SIMILAR OBJECTIVES

Slide 137

Slide 137 text

Architecture is about Intent

Slide 138

Slide 138 text

Intent = what the SYSTEM DOES

Slide 139

Slide 139 text

reflect THE USER’S MENTAL MODEL in your architecture

Slide 140

Slide 140 text

domain as a 1st class citizen

Slide 141

Slide 141 text

ONE MORE THING…

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

we are hiring fabiano@magne/s.com.br magne/s.com.br/dev

Slide 145

Slide 145 text

Developer #1 Developer #2 The Boss The Mastermind Tio Roberto Domain Father Advisor Presenter CAST TYRION LANNISTER Jon SNOW SHERKLOCKS’ BROTHER IVAR JACOBSON UNCLE BOB ERIC EVANS ROBERT DUVAL FABIANO BESELGA by @fbzga