Successfully reported this slideshow.

DCI with Ruby and Rails

11

Share

1 of 27
1 of 27

DCI with Ruby and Rails

11

Share

Download to read offline

Description

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.

Transcript

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

Description

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.

Transcript

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

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

×