Skip to content

Dependency Injection Fails with mixed classes and structs when depending on an interface #512

@syeopite

Description

@syeopite

A service that depends on an interface cannot receive additional injected implementations of the interface when the first implementation is a Struct and the additional implementation is a Class

module Interface
  abstract def foo
end

@[ADI::Register]
@[ADI::AsAlias(Interface)]
struct Dependency
  include Interface

  def foo
    return "Dependency"
  end
end

@[ADI::Register]
@[ADI::AsAlias(Interface)]
class InjectedDependency
  include Interface

  def foo
    return "Injected Dependency"
  end
end

@[ADI::Register(public: true)]
class Service
  def initialize(@service : Interface)
  end

  def run
    return @service.foo
  end
end

puts ADI.container.service.run # => "Dependency"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions