class Athena::DependencyInjection::ServiceLocator(T)
inherits Reference
#
Provides a set of services from the container by their ID, only instantiating a service once it is requested.
Useful when a service only needs some of many services at runtime, e.g. based on the request, so that the others are not instantiated.
A service locator is injected into a parameter typed as ADI::ServiceLocator(T), where T is a type every provided service is compatible with.
Its value is a hash of the services it provides, keyed by the ID they are requested with:
module FormatterInterface
abstract def format(data : String) : String
end
@[ADI::Register]
class JSONFormatter
include FormatterInterface
def format(data : String) : String
%({"data": "#{data}"})
end
end
@[ADI::Register]
class XMLFormatter
include FormatterInterface
def format(data : String) : String
"<data>#{data}</data>"
end
end
@[ADI::Register(public: true, _formatters: {"json" => "@json_formatter", "xml" => "@xml_formatter"})]
class Renderer
def initialize(@formatters : ADI::ServiceLocator(FormatterInterface)); end
def render(format : String, data : String) : String
# Only the formatter of the requested format is instantiated.
@formatters.get(format).format data
end
end
ADI.container.renderer.render "xml", "foo" # => "<data>foo</data>"
Within a bundle extension, the hash is the value of the parameter, e.g. parameters: {formatters: {value: {"json" => "@json_formatter"}}}.
Constructors#
.new(factories : Hash(String, Proc(T)))#
Creates a locator providing the services returned by the provided factories, keyed by their ID.
Methods#
#get(id : String) : T#
Returns the service with the provided id, instantiating it if it has not been already.
Raises a KeyError if this locator does not provide a service with that id.
#get?(id : String) : T | Nil#
Returns the service with the provided id, instantiating it if it has not been already, or nil if this locator does not provide it.
#has?(id : String) : Bool#
Returns true if this locator provides a service with the provided id, otherwise false.
#provided_services : Array(String)#
Returns the IDs of the services this locator provides.