Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

DCI with Ruby and Rails

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.

  • Be the first to comment

DCI with Ruby and Rails

  1. 1. DCIData Context InteractionAbout me:Dawid SklodowskiWorks at @HouseTrip (we are hiring)
  2. 2. DCI: Why?Separating behaviour from data modelMaking behaviour first-class citizenSupporting style of thinking closer topeople’s mental model
  3. 3. DCI: Who?Invented by Trygve ReenskaugCurrent formulation mostly by TrygveReenskaug and Jim CoplienIn 2009
  4. 4. ExampleNinja Elf kills Pirate Dwarf
  5. 5. 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 beforedeciding who dies.
  6. 6. Lets leave the elfobject aloneHe doesn’t need to know how killing isimplemented in system.Lets implement activities (behaviours) for ourcharacters.
  7. 7. Data
  8. 8. DataData is “what system is”.Objects which contain methods to changetheir state.No interaction with other objects
  9. 9. ContextEntry pointInjects rolesKnows who does what
  10. 10. InteractionInteraction is “what system does”.Implemented as roles which are played byobjects in run-time
  11. 11. MVC and DCI(how can it play with Rails)DCI can be complementary to MVCController loads adequate objects andinitiates context with themContext injects roles to objects and let themto play
  12. 12. MVC and DCIControllerUserContextModelViewRolesdata object
  13. 13. Use Case: Context
  14. 14. Sample Role
  15. 15. How to implement?with RubyProblem: Role Injection
  16. 16. How to implement1. Decoration: #extendCons:There is no#unextend methodTends to be slowon some Rubies
  17. 17. How to implement2. PresenterPros:Object encapsulationCons:Object encapsulation(however object stillaccessible via#object method)
  18. 18. How to implement3. DelegationCons:Uses#method_missing toimplement delegation
  19. 19. How to implement4. Delegate ClassIt creates delegation methods for instance methods of delegatedclass.Cons:Static delegationrelationshipRole is a classinstead of module
  20. 20. How to implementDynamic DelegateClassDynamic delegation relationship, but isn’t that slow?
  21. 21. How to implementAny pattern can be implemented in dynamic way
  22. 22. Performance comparisonImplementation
  23. 23. PerformancecomparisonResults
  24. 24. How to implementDelegateClass: Performance WiseCaching anonymous role classes
  25. 25. Other prosWe can chain roles on objectWe don’t pollute original object with roleleftovers (unlike #extend)
  26. 26. SummarySeparates behaviour from data modelCan play nicely with Rails and MVCNo straightforward way to implement withRubyBut it is doable
  27. 27. Thank