stop! use case time!

23
STOP! Use case time! Or, a brief introduction to DCI. Wednesday, January 9, 13

Upload: andre-medeiros

Post on 31-Oct-2014

431 views

Category:

Technology


2 download

DESCRIPTION

A brief introduction to DCI

TRANSCRIPT

Page 1: STOP! Use case time!

STOP! Use case time!Or, a brief introduction to DCI.

Wednesday, January 9, 13

Page 2: STOP! Use case time!

Who am I?André Medeiros

Rubyist for 7+ years

@superdealloc

github.com/andremedeiros

quintel.com

Wednesday, January 9, 13

Page 3: STOP! Use case time!

You’ve probably heard of it

Wednesday, January 9, 13

Page 4: STOP! Use case time!

You’ve probably heard of it

Wednesday, January 9, 13

Page 5: STOP! Use case time!

You’ve probably heard of it

Wednesday, January 9, 13

Page 6: STOP! Use case time!

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

Page 7: STOP! Use case time!

Wednesday, January 9, 13

Page 8: STOP! Use case time!

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

Page 9: STOP! Use case time!

DCI is great for this approachInvented 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

Page 10: STOP! Use case time!

D is for DataWhat the object is

Persistence

Structure

Wednesday, January 9, 13

Page 11: STOP! Use case time!

C is for Cookie ContextClass 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

Page 12: STOP! Use case time!

I is for InteractionWhat 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

Page 13: STOP! Use case time!

How it worksController Contextstarts use case

Wednesday, January 9, 13

Page 14: STOP! Use case time!

How it worksController Contextstarts use case

Context Object(s)finds or creates theparticipating objects

Wednesday, January 9, 13

Page 15: STOP! Use case time!

How it worksController Contextstarts use case

Context Object(s)

mixes in all the roles neededRole

RoleRole

Wednesday, January 9, 13

Page 16: STOP! Use case time!

How it worksController Contextstarts use case

Context Object(s)

invokes the role methodsRole

RoleRole

Wednesday, January 9, 13

Page 17: STOP! Use case time!

Code Samples

Wednesday, January 9, 13

Page 18: STOP! Use case time!

class TransfersController def create @source = Account.find(params[:source_id]) @destination = Account.find(params[:dest_id])

@source.balance -= amount @destination.balance += amount endend

Wednesday, January 9, 13

Page 19: STOP! Use case time!

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 endend

Wednesday, January 9, 13

Page 20: STOP! Use case time!

AdvantagesKeeps the model slim (fat != obese)

Business logic is easy to find and track down

Facilitates reusability

Highly testable

Wednesday, January 9, 13

Page 21: STOP! Use case time!

FUDObject#extend kills kittens

Wednesday, January 9, 13

Page 22: STOP! Use case time!

FUDConcerns do the exact same thing

It’s not real OO

Makes you write more code

Wednesday, January 9, 13

Page 23: STOP! Use case time!

Questions?

Wednesday, January 9, 13