Telling not-asking

886 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
886
On SlideShare
0
From Embeds
0
Number of Embeds
126
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Telling not-asking

  1. 1. Telling, not asking@kevinrutherford
  2. 2. “Tell, dont ask”… means WHAT, exactly … ?
  3. 3. “Tell, dont ask”"Procedural code gets informationthen makes decisions. Object-orientedcode tells objects to do things."— Alec Sharp, Smalltalk by Example
  4. 4. How can I do that?
  5. 5. How can I do that?… without violating the SRP?
  6. 6. How can I do that?… without violating the SRP?… without having DoubleDispatch everywhere?
  7. 7. controllers,viewsA simple to-do list apprake tasks,test scriptsetcuse caseobjects
  8. 8. A use case objectclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?event = Event.create!(summary: "##{action.id} created", project_id: project.id)msg = "#{@user.name} created #{action.title}"project.campfire_room.speak(msg)PushMessage.instance.publish("action-do.#{action.project.id}.action_item",{:action_item_id => action})action.project.users.reject {|u| u == @user}.each do |u|ActionItemMailer.action_item_created(action, u).deliverendendactionendend
  9. 9. A use case objectclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?event = Event.create!(summary: "##{action.id} created", project_id: project.id)msg = "#{@user.name} created #{action.title}"project.campfire_room.speak(msg)PushMessage.instance.publish("action-do.#{action.project.id}.action_item",{:action_item_id => action})action.project.users.reject {|u| u == @user}.each do |u|ActionItemMailer.action_item_created(action, u).deliverendendactionendend
  10. 10. A use case objectclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?event = Event.create!(summary: "##{action.id} created", project_id: project.id)msg = "#{@user.name} created #{action.title}"project.campfire_room.speak(msg)PushMessage.instance.publish("action-do.#{action.project.id}.action_item",{:action_item_id => action})action.project.users.reject {|u| u == @user}.each do |u|ActionItemMailer.action_item_created(action, u).deliverendendactionendend
  11. 11. A use case objectclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?event = Event.create!(summary: "##{action.id} created", project_id: project.id)msg = "#{@user.name} created #{action.title}"project.campfire_room.speak(msg)PushMessage.instance.publish("action-do.#{action.project.id}.action_item",{:action_item_id => action})action.project.users.reject {|u| u == @user}.each do |u|ActionItemMailer.action_item_created(action, u).deliverendendactionendend
  12. 12. A use case objectclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?event = Event.create!(summary: "##{action.id} created", project_id: project.id)msg = "#{@user.name} created #{action.title}"project.campfire_room.speak(msg)PushMessage.instance.publish("action-do.#{action.project.id}.action_item",{:action_item_id => action})action.project.users.reject {|u| u == @user}.each do |u|ActionItemMailer.action_item_created(action, u).deliverendendactionendend
  13. 13. A simple to-do list app
  14. 14. “Tell, dont ask”"Procedural code gets informationthen makes decisions. Object-orientedcode tells objects to do things."— Alec Sharp, Smalltalk by Example
  15. 15. “Tell, dont ask”"Procedural code gets informationthen makes decisions. Object-orientedcode tells objects to do things."— Alec Sharp, Smalltalk by Example
  16. 16. “Tell, dont ask”"Sometimes an object will changeits state, and on those occasionsit might be expected to reportthat change to others."— David West, Object Thinking
  17. 17. A use case objectclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?event = Event.create!(summary: "##{action.id} created", project_id: project.id)msg = "#{@user.name} created #{action.title}"project.campfire_room.speak(msg)PushMessage.instance.publish("action-do.#{action.project.id}.action_item",{:action_item_id => action})action.project.users.reject {|u| u == @user}.each do |u|ActionItemMailer.action_item_created(action, u).deliverendendactionendend
  18. 18. A use case objectclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?event = Event.create!(summary: "##{action.id} created", project_id: project.id)msg = "#{@user.name} created #{action.title}"project.campfire_room.speak(msg)PushMessage.instance.publish("action-do.#{action.project.id}.action_item",{:action_item_id => action})action.project.users.reject {|u| u == @user}.each do |u|ActionItemMailer.action_item_created(action, u).deliverendendactionendend
  19. 19. A use case object : afterclass CreateActiondef initialize(user)@user = userenddef execute(options)project = @user.open_project(options[:project_id])action = project.create_action(options[:attrs])unless action.errors.any?EventBus.announce(action-created, action: action, actor: @user, publisher: self)endactionendendhttps://rubygems.org/gems/event_bus
  20. 20. The Campfire listenerclass CampfireAnnouncerdef action_created(payload)actor = payload[:actor]action = payload[:action]project = action.projectmsg = "#{actor.name} created #{action.title}"project.campfire_room.speak(msg)enddef action_deleted(payload)#...end#...end
  21. 21. Listener registrationclass ApplicationController < ActionController::Basebefore_filter doEventBus.clearEventBus.register(CampfireAnnouncer.new)EventBus.register(EventLogger.new)EventBus.register(EmailNotifier.new)EventBus.register(PushNotifier.new)end#...end
  22. 22. A simple to-do list app
  23. 23. A simple to-do list applistenersevent bus
  24. 24. “Tell, dont ask”Tell, dont ...
  25. 25. “Tell, dont ask”Tell, dont wait to be asked
  26. 26. “Tell, dont ask”Tell, dont wait to be asked@kevinrutherfordhttp://silkandspinach.nethttp://refactoringinruby.infohttps://rubygems.org/gems/event_bus

×