6. You’ve probably heard of it
“DCI in Ruby is completely broken”
- Tony Arcieri (@bascule)
“Rails Developers Should Take DCI Seriously”
- Giles Bowkett (@gilesgoatboy)
Wednesday, January 9, 13
8. How I develop apps
• UI first approach
• UI directly maps to operations within the code
• MVC doesn’t always conform to this standard of
development
Wednesday, January 9, 13
9. DCI is great for this approach
Invented by Trygve Reenskaug
Comes in where MVC fails: capturing behavior
Places interaction in obvious places
Models represent entities, not behavior
Splits what an object is from what it does
Wednesday, January 9, 13
10. D is for Data
What the object is
Persistence
Structure
Wednesday, January 9, 13
11. C is for Cookie Context
Class which enacts one or more use cases
Instantiated by a user action
Mixes in participating objects with Roles
Responsible for acting out the use case
Wednesday, January 9, 13
12. I is for Interaction
What the system does
Logic is contained in Role modules
Implemented by mixing in objects with Roles in a given
Context or use case
Wednesday, January 9, 13
13. How it works
Controller starts use case Context
Wednesday, January 9, 13
14. How it works
Controller starts use case Context
finds or creates the
Context participating objects Object(s)
Wednesday, January 9, 13
15. How it works
Controller starts use case Context
Context Object(s)
mixes in all the roles needed
Role
Role
Role
Wednesday, January 9, 13
16. How it works
Controller starts use case Context
Context Object(s)
invokes the role methods
Role
Role
Role
Wednesday, January 9, 13
18. class TransfersController
def create
@source = Account.find(params[:source_id])
@destination = Account.find(params[:dest_id])
@source.balance -= amount
@destination.balance += amount
end
end
Wednesday, January 9, 13
19. class MoneyTransfer
def initialize(source, destination)
@source = source
@destination = destination
assign_transferrer(@source)
end
def execute(amount)
@source.transfer_to(@destination, amount)
end
private
def assign_transferrer(account)
account.extend(Transferrer)
end
module Transferrer
def transfer_to(destination, amount)
self.balance -= amount
destination.balance += amount
end
end
end
Wednesday, January 9, 13
20. Advantages
Keeps the model slim (fat != obese)
Business logic is easy to find and track down
Facilitates reusability
Highly testable
Wednesday, January 9, 13
21. FUD
Object#extend kills kittens
Wednesday, January 9, 13
22. FUD
Concerns do the exact same thing
It’s not real OO
Makes you write more code
Wednesday, January 9, 13