Hollywood Principle

Pawel Pierzchala @zwrozka

DRUG 20.05.2013

About people

and hotel rooms

People

class PeopleController
  def create
    person_repository.add(params)
  end

  private

  def person_repository
    PersonRepository.new(current_trip)
  end
end

Rooms

yet another resource

Room reservations

class RoomAssignmentsController
  def create
    room_assignments.add(params)
  end

  private

  def room_assignments
    RoomAssignments.new(person_repository, room_repository)
  end
end

Hollywood Principle

don't call us, we'll call you

Inversion of Control

Dependency Injection

Benefits

  • replacabilty
  • testability
  • isolated tests

Dependency Injection

def person_repository(current_trip)
  PersonRepository.new(current_trip)
end

def room_assignments
  RoomAssignments.new(person_repository, room_repository)
end
 # noop

almost noop

class Injector
  include Dependor::AutoInject

  attr_reader :current_trip

  def initalize(current_trip)
    @current_trip = current_trip
  end
end

Entry points

def room_assignments
  RoomAssignments.new(person_repository, room_repository)
end
def room_assignments
  Injector.new(current_trip).room_assignments
end
inject :room_assignments

a little bit of config

class ApplicationController
  extend Dependor::Injectable

  def injector
    Injector.new(current_trip)
  end
end

Constructors

class RoomAssignments
  def initalize(person_repository, rooms_repository)
    @person_repository = person_repository
    @room_repository = room_repository
  end

  private

  attr_reader :person_repository, :room_repository
end
class RoomAssignments
  takes :person_repository, :room_repository
end

Summary

  • dependor on github
  • makes dependecies visible
  • loose coupling always helps with testing
  • helps with deep dependencies
  • emphasies composability

Thanks

Any questions?