More Related Content

Recently uploaded(20)


DCI with Ruby and Rails

  1. DCI Data Context Interaction About me: Dawid Sklodowski Works at @HouseTrip (we are hiring)
  2. DCI: Why? Separating behaviour from data model Making behaviour first-class citizen Supporting style of thinking closer to people’s mental model
  3. DCI: Who? Invented by Trygve Reenskaug Current formulation mostly by Trygve Reenskaug and Jim Coplien In 2009
  4. Example Ninja Elf kills Pirate Dwarf
  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 before deciding who dies.
  6. 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.
  7. Data
  8. Data Data is “what system is”. Objects which contain methods to change their state. No interaction with other objects
  9. Context Entry point Injects roles Knows who does what
  10. Interaction Interaction is “what system does”. Implemented as roles which are played by objects in run-time
  11. 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
  12. MVC and DCI ControllerUser Context Model View Rolesdata object
  13. Use Case: Context
  14. Sample Role
  15. How to implement? with Ruby Problem: Role Injection
  16. How to implement 1. Decoration: #extend Cons: There is no #unextend method Tends to be slow on some Rubies
  17. How to implement 2. Presenter Pros: Object encapsulation Cons: Object encapsulation (however object still accessible via #object method)
  18. How to implement 3. Delegation Cons: Uses #method_missing to implement delegation
  19. How to implement 4. Delegate Class It creates delegation methods for instance methods of delegated class. Cons: Static delegation relationship Role is a class instead of module
  20. How to implement Dynamic DelegateClass Dynamic delegation relationship, but isn’t that slow?
  21. How to implement Any pattern can be implemented in dynamic way
  22. Performance comparison Implementation
  23. Performance comparison Results
  24. How to implement DelegateClass: Performance Wise Caching anonymous role classes
  25. Other pros We can chain roles on object We don’t pollute original object with role leftovers (unlike #extend)
  26. Summary Separates behaviour from data model Can play nicely with Rails and MVC No straightforward way to implement with Ruby But it is doable
  27. Thank You @DawidSklodowski