module Athena::DependencyInjection::Injectable

Overview

Adds a new constructor that resolves the required services based on type and name.

Can be included into a class/struct in order to automatically inject the required services from the container based on the type's initializer.

Service lookup is based on the type restriction and name of the initializer arguments. If there is only a single service of the required type, then that service is used. If there are multiple services of the required type then the name of the argument's name is used. An exception is raised if a service was not able to be resolved.

Examples

Default Usage

@[ADI::Register]
class Store
  include ADI::Service

  property uuid : String = "UUID"
end

class MyNonService
  include ADI::Injectable

  getter store : Store

  def initialize(@store : Store); end
end

MyNonService.new.store.uuid # => "UUID"

Non Service Dependencies

Named arguments take precedence. This allows dependencies to be supplied explicitly without going through the resolving process; such as for testing.

@[ADI::Register]
class Store
  include ADI::Service

  property uuid : String = "UUID"
end

class MyNonService
  include ADI::Injectable

  getter store : Store
  getter id : String

  def initialize(@store : Store, @id : String); end
end

service = MyNonService.new(id: "FOO")
service.store.uuid # => "UUID"
service.id         # => "FOO"

Defined in:

athena-dependency_injection.cr