STOP! Use case time!

Preview:

DESCRIPTION

A brief introduction to DCI

Citation preview

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

Wednesday, January 9, 13

Who am I?André Medeiros

Rubyist for 7+ years

@superdealloc

github.com/andremedeiros

quintel.com

Wednesday, January 9, 13

You’ve probably heard of it

Wednesday, January 9, 13

You’ve probably heard of it

Wednesday, January 9, 13

You’ve probably heard of it

Wednesday, January 9, 13

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

Wednesday, January 9, 13

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

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

D is for DataWhat the object is

Persistence

Structure

Wednesday, January 9, 13

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

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

How it worksController Contextstarts use case

Wednesday, January 9, 13

How it worksController Contextstarts use case

Context Object(s)finds or creates theparticipating objects

Wednesday, January 9, 13

How it worksController Contextstarts use case

Context Object(s)

mixes in all the roles neededRole

RoleRole

Wednesday, January 9, 13

How it worksController Contextstarts use case

Context Object(s)

invokes the role methodsRole

RoleRole

Wednesday, January 9, 13

Code Samples

Wednesday, January 9, 13

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

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

AdvantagesKeeps the model slim (fat != obese)

Business logic is easy to find and track down

Facilitates reusability

Highly testable

Wednesday, January 9, 13

FUDObject#extend kills kittens

Wednesday, January 9, 13

FUDConcerns do the exact same thing

It’s not real OO

Makes you write more code

Wednesday, January 9, 13

Questions?

Wednesday, January 9, 13