DCI Data Context Interaction About me: Dawid Sklodowski Works at @HouseTrip (we are hiring)
Jun 20, 2015
DCIData Context Interaction
About me:
Dawid Sklodowski
Works at @HouseTrip (we are hiring)
DCI: Why?
Separating behaviour from data model
Making behaviour first-class citizen
Supporting style of thinking closer to people’s mental model
DCI: Who?
Invented by Trygve Reenskaug
Current formulation mostly by Trygve Reenskaug and Jim Coplien
In 2009
Example
Ninja Elf kills Pirate Dwarf
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.
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.
Data
Data
Data is “what system is”.
Objects which contain methods to change their state.
No interaction with other objects
Context
Entry point
Injects roles
Knows who does what
Interaction
Interaction is “what system does”.
Implemented as roles which are played by objects in run-time
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
MVC and DCI
ControllerUser
Context
ModelView
Rolesdata object
Use Case: Context
Sample Role
How to implement?with Ruby
Problem: Role Injection
How to implement1. Decoration: #extend
Cons:
There is no #unextend method
Tends to be slow on some Rubies
How to implement2. Presenter
Pros:
Object encapsulation
Cons:
Object encapsulation (however object still accessible via #object method)
How to implement3. Delegation
Cons:
Uses #method_missing to implement delegation
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
How to implementDynamic DelegateClass
Dynamic delegation relationship, but isn’t that slow?
How to implementAny pattern can be implemented in dynamic way
Performance comparisonImplementation
Performance comparison
Results
How to implementDelegateClass: Performance Wise
Caching anonymous role classes
Other pros
We can chain roles on object
We don’t pollute original object with role leftovers (unlike #extend)
Summary
Separates behaviour from data model
Can play nicely with Rails and MVC
No straightforward way to implement with Ruby
But it is doable
Thank You
github.com/dawid-sklodowskionceuponaline.info@DawidSklodowski