dry-rb / dry-auto_inject

Container-agnostic constructor injection mixin

Home Page:https://dry-rb.org/gems/dry-auto_inject/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Builder interface

dsisnero opened this issue · comments

I take dependency injection as a means to enable easy ability to swap out classes. This does provide that because we have constructor injection. However, the examples I have seen show that we are using it with a specific class. I would like to see more examples of using it with abstract classes and building the interfaces.

require 'dry/types'

Types = Dry.Types()
adapter = Types.Interface(:broadcast, :subscribe)

class Main
include Import[adapter: 'adapters.async_adapter'] # how to insure adapter satisfies adapter interface?

def subscribe(name)
adapter.subscribe(name)
end

def mail
adapter.broadcast.each do |n|
mail(n)
end
end

end

so, would like a method to build with a different adapter - from container so, instead of
redis = Container('adapter.redis_adapter')
redis = Main.new(adapter: redis)

would like something like this:
redis_main = Main.build_with(adapter: 'adapter.redis_adapter') # will search container for redis adapter

Code idea from: http://gafur.me/2018/01/01/strategy-pattern-in-the-hanami-events-code.html

Please start a thread on discourse.dry-rb.org if you'd like to discuss a new feature proposal. Issues on GitHub are used for bug reports and approved feature requests only. See CONTRIBUTING.md for more information.