Slide 1

Slide 1 text

CLEAN ARCHITECTURE ON RAILS

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

It’s impressive how this talk spread like wild fire!

Slide 4

Slide 4 text

CHAPTER I What is Clean Architecture?

Slide 5

Slide 5 text

but, who am I?

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

bit.ly/clean-rails

Slide 8

Slide 8 text

CHAPTER I What is Clean Architecture?

Slide 9

Slide 9 text

the PROBLEM

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

your top level structure is screaming the web FRAMEWORK

Slide 12

Slide 12 text

The web is a delivery mechanism

Slide 13

Slide 13 text

the web is a COMMODITY

Slide 14

Slide 14 text

yet it DOMINATES your code

Slide 15

Slide 15 text

image you’ve just joined a PROJECT

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

what makes a GOOD architecture?

Slide 19

Slide 19 text

it is not about frameworks and tools

Slide 20

Slide 20 text

IVAR JACOBSON

Slide 21

Slide 21 text

business rules INTERACTORS (aka use cases)

Slide 22

Slide 22 text

domain model ENTITIES

Slide 23

Slide 23 text

communication interfaces BOUNDARIES

Slide 24

Slide 24 text

diagrams: Robert C Martin

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

What about MVC?

Slide 31

Slide 31 text

MVC as a Web architecture

Slide 32

Slide 32 text

this is NOT WHAT I MEANT

Slide 33

Slide 33 text

WEB FRAMEWORK APP

Slide 34

Slide 34 text

CHAPTER II HOW to use it with Rails?

Slide 35

Slide 35 text

BRAAVOS

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

WHAT ABOUT THIS ONE?

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

HIRED!

Slide 44

Slide 44 text

first day at WORK

Slide 45

Slide 45 text

THE SYSTEM MUST TRANSFER MONEY BETWEEN THE FAMILIES ACCOUNTS

Slide 46

Slide 46 text

OK! ANY EXCEPTION COURSES?

Slide 47

Slide 47 text

WHEN THERE ISN’T ENOUGH MONEY TO TRANSFER.

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

GREAT! YOU HAVE 10 MINUTES!

Slide 50

Slide 50 text

challenge accepted!

Slide 51

Slide 51 text

1 create the PROJECT

Slide 52

Slide 52 text

rails new braavos

Slide 53

Slide 53 text

mkdir core

Slide 54

Slide 54 text

module Core end core.rb

Slide 55

Slide 55 text

2 define BOUNDARIES

Slide 56

Slide 56 text

README Driven Development

Slide 57

Slide 57 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 58

Slide 58 text

3 define behaviour Driven Development

Slide 59

Slide 59 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 60

Slide 60 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 61

Slide 61 text

FFF

Slide 62

Slide 62 text

define DATA MODEL 4

Slide 63

Slide 63 text

CAN YOU TELL ME A LITTLE MORE ABOUT THE DOMAIN?

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

trades debit: -1000 credit: +1000

Slide 67

Slide 67 text

the ENTRY POINT 5

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

the USE CASE 6 Image: HENRY HEMMING

Slide 70

Slide 70 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 71

Slide 71 text

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

REFACTOR!

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

CAZE DSL to elegantly declare use cases

Slide 76

Slide 76 text

Gemfile gem 'caze'

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

g github.com/magne/s/caze

Slide 80

Slide 80 text

BREAKING NEWS! 
 WE HAVE MONEY TO HIRE AGAIN!

Slide 81

Slide 81 text

WHAT ABOUT THIS SNOW?

Slide 82

Slide 82 text

WHAT?

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 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 86

Slide 86 text

No content

Slide 87

Slide 87 text

expose it to the web 7

Slide 88

Slide 88 text

let’s call it on a CONTROLLER

Slide 89

Slide 89 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 90

Slide 90 text

No content

Slide 91

Slide 91 text

bit.ly/braavos

Slide 92

Slide 92 text

what just happened?

Slide 93

Slide 93 text

Core represents WHAT THE SYSTEM Does CHANGES

Slide 94

Slide 94 text

core/ get_balance.rb transfer_money.rb BEHAVIOUR CHANGES

Slide 95

Slide 95 text

the models represent WHAT THE SYSTEM IS STABLE

Slide 96

Slide 96 text

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

Slide 97

Slide 97 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 98

Slide 98 text

Core represents WHAT THE SYSTEM Does CHANGES

Slide 99

Slide 99 text

the models represent WHAT THE SYSTEM IS STABLE

Slide 100

Slide 100 text

Models Core Rails delivery mechanism behaviour MODULE data

Slide 101

Slide 101 text

the ALTERNATIVES

Slide 102

Slide 102 text

Models Core Rails delivery mechanism behaviour RAILS ENGINE

Slide 103

Slide 103 text

The database is a detail

Slide 104

Slide 104 text

Isolate it!

Slide 105

Slide 105 text

Models Core Rails delivery mechanism behaviour gem rails engine data

Slide 106

Slide 106 text

Core Web Models UI DB Controllers Views

Slide 107

Slide 107 text

but what makes a GOOD architecture?

Slide 108

Slide 108 text

CLEAN ARCHITECTURE HEXAGONAL ARCHITECTURE DOMAIN DRIVEN DESIGN DATA CONTEXT INTERACTION TRAILBLAZER

Slide 109

Slide 109 text

all these architectures have SIMILAR OBJECTIVES

Slide 110

Slide 110 text

what about real life?

Slide 111

Slide 111 text

magnetis.com.br

Slide 112

Slide 112 text

~ 4 years old codebase

Slide 113

Slide 113 text

experimenting with clean architecture ~ 2 years

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

the first seeds magnetis core

Slide 116

Slide 116 text

a module for each DOMAIN CONCEPT

Slide 117

Slide 117 text

we have a module for investments recommendation Advisor

Slide 118

Slide 118 text

Advisor.recommend_investments(params)

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

we have a module for accounting Accountant

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

magnetis/ domain/ accountant/ advisor/

Slide 123

Slide 123 text

BOUNDED CONTEXTs Advisor Accountant Order Manager Account Manager Trader Pricing Backtesting

Slide 124

Slide 124 text

why not MICROSERVICES?

Slide 125

Slide 125 text

we also use the Rails WAY

Slide 126

Slide 126 text

FINAL CHAPTER when to use it?

Slide 127

Slide 127 text

YOUR PRODUCT

Slide 128

Slide 128 text

core business your product rails way clean architecture

Slide 129

Slide 129 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 130

Slide 130 text

THE distance from the view

Slide 131

Slide 131 text

the domain logic is close to the view Rails way

Slide 132

Slide 132 text

the domain logic is far from the view Clean architecture

Slide 133

Slide 133 text

TIMING

Slide 134

Slide 134 text

image: Kent Beck

Slide 135

Slide 135 text

Lessons learned

Slide 136

Slide 136 text

no good

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

getting out of the rails conventions may be hard

Slide 139

Slide 139 text

the cool STUFF

Slide 140

Slide 140 text

defer decisions until the last responsible moment

Slide 141

Slide 141 text

it SCREAMS BEHAVIOR

Slide 142

Slide 142 text

decoupled business rules

Slide 143

Slide 143 text

tests RUNNING FASTER!

Slide 144

Slide 144 text

what makes a GOOD architecture?

Slide 145

Slide 145 text

Architecture is about Intent

Slide 146

Slide 146 text

Intent = what the SYSTEM DOES

Slide 147

Slide 147 text

reflect THE USER’S MENTAL MODEL in your architecture

Slide 148

Slide 148 text

domain as a 1st class citizen

Slide 149

Slide 149 text

ONE MORE THING…

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

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

Slide 153

Slide 153 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