Top Banner
DCI Data Context Interaction About me: Dawid Sklodowski Works at @HouseTrip (we are hiring)
27

DCI with Ruby and Rails

Jun 20, 2015

Download

Technology

Data Context Interaction is recently invented programming paradigm, which aims at separating behaviour from data model, by extracting interactions into roles, which can be played by objects in various contexts.

This presentation is going to give brief introduction to DCI, propose ways to implement roles' injection in Ruby and discuss how DCI could be used to supplement Rails' MVC paradigm.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DCI with Ruby and Rails

DCIData Context Interaction

About me:

Dawid Sklodowski

Works at @HouseTrip (we are hiring)

Page 2: DCI with Ruby and Rails

DCI: Why?

Separating behaviour from data model

Making behaviour first-class citizen

Supporting style of thinking closer to people’s mental model

Page 3: DCI with Ruby and Rails

DCI: Who?

Invented by Trygve Reenskaug

Current formulation mostly by Trygve Reenskaug and Jim Coplien

In 2009

Page 4: DCI with Ruby and Rails

Example

Ninja Elf kills Pirate Dwarf

Page 5: DCI with Ruby and Rails

Elf object would...

Reduce victim’s health points to 0.

Add some experience to itself.

Log frag on leader board.

Actually it should check who gets killed.

Or... maybe we should have fight before deciding who dies.

Page 6: DCI with Ruby and Rails

Lets leave the elf object alone

He doesn’t need to know how killing is implemented in system.

Lets implement activities (behaviours) for our characters.

Page 7: DCI with Ruby and Rails

Data

Page 8: DCI with Ruby and Rails

Data

Data is “what system is”.

Objects which contain methods to change their state.

No interaction with other objects

Page 9: DCI with Ruby and Rails

Context

Entry point

Injects roles

Knows who does what

Page 10: DCI with Ruby and Rails

Interaction

Interaction is “what system does”.

Implemented as roles which are played by objects in run-time

Page 11: DCI with Ruby and Rails

MVC and DCI(how can it play with Rails)

DCI can be complementary to MVC

Controller loads adequate objects and initiates context with them

Context injects roles to objects and let them to play

Page 12: DCI with Ruby and Rails

MVC and DCI

ControllerUser

Context

ModelView

Rolesdata object

Page 13: DCI with Ruby and Rails

Use Case: Context

Page 14: DCI with Ruby and Rails

Sample Role

Page 15: DCI with Ruby and Rails

How to implement?with Ruby

Problem: Role Injection

Page 16: DCI with Ruby and Rails

How to implement1. Decoration: #extend

Cons:

There is no #unextend method

Tends to be slow on some Rubies

Page 17: DCI with Ruby and Rails

How to implement2. Presenter

Pros:

Object encapsulation

Cons:

Object encapsulation (however object still accessible via #object method)

Page 18: DCI with Ruby and Rails

How to implement3. Delegation

Cons:

Uses #method_missing to implement delegation

Page 19: DCI with Ruby and Rails

How to implement4. Delegate Class

It creates delegation methods for instance methods of delegated class.

Cons:

Static delegation relationship

Role is a class instead of module

Page 20: DCI with Ruby and Rails

How to implementDynamic DelegateClass

Dynamic delegation relationship, but isn’t that slow?

Page 21: DCI with Ruby and Rails

How to implementAny pattern can be implemented in dynamic way

Page 22: DCI with Ruby and Rails

Performance comparisonImplementation

Page 23: DCI with Ruby and Rails

Performance comparison

Results

Page 24: DCI with Ruby and Rails

How to implementDelegateClass: Performance Wise

Caching anonymous role classes

Page 25: DCI with Ruby and Rails

Other pros

We can chain roles on object

We don’t pollute original object with role leftovers (unlike #extend)

Page 26: DCI with Ruby and Rails

Summary

Separates behaviour from data model

Can play nicely with Rails and MVC

No straightforward way to implement with Ruby

But it is doable

Page 27: DCI with Ruby and Rails

Thank You

github.com/dawid-sklodowskionceuponaline.info@DawidSklodowski