These are chat archives for dry-rb/chat
Question - I’m looking at using dry-rb to build a re-usable gem that allows the consuming application to define & configure the persistenance layer, and other infrastructure related dependencies. I’ve got this more or less working when I explictly ask the container to resolve the dependency just before I need the service.
class SaveMyEntity def call(entity) my_repo = MyContainer[‘persistence.my_repo’] my_repo.save(entity) end end
I’m wondering if I can also use either dry-auto_inject and/or dry-system to also do this? However, from what I can see, using the auto loading and the auto injector work differently.
Import = Dry::AutoInject(MyContainer) class SaveMyEntity include Import[‘persistence.my_repo’] # throws Dry::System::ComponentLoadError: could not load component end
Dry::AutoInjectseem to behave differently. My mistake, the above example does work, but the Application.injector (suggested by the dry-system example), throws the above exception.
Import = Dry::AutoInject(Container) # works Import = Container.injector # throws Dry::System::ComponentLoadError: could not load component
I’m currently calling one of those 2 lines inside a module pretty much when the gem is initially loaded. The first line seems to work, but the 2nd throws the error when I use the Import in a subsequent ruby file. Should I be deferrring these until after the consuming application has a chance to register it’s own dependencies?
Basically, just trying to figure out the correct pattern to use moving forward. I’m not sure, if I should be bothering with dry-system in my case or just leverage dry-container and possibly dry-auto_inject.
# Require the container require_relative "main/container" # Load files with manually registered dependencies Main::Container.require "component/container/persistence" # Finalize the container, which does auto-registration if you've configured it Main::Container.finalize!