Hello, friends.
First of all, pardon if my question is stupid. I’m not an advanced rubyist and this is my first contact with IoC containers.
I have a Rails app that computes a complex calculation on which I need do forward parameters for many stack levels. So to reduce complexity I’m trying to use a dry-container
to register these parameters so they will be resolved using dry-auto_inject
inside any layer of the calculation.
This is my container class:
module MyModule
module Container
def self.configure(data)
@container.register :foo, data.foo
@container.register :bar, data.bar
end
@container = Dry::Container.new
AutoInject = Dry::AutoInject(@container)
end
end
I’m trying to use this approach on the following calculation:
module MyModule
class Calculator
def self.calculate(document)
Container.configure(document)
{
foo: FooCalculator.calculate,
bar: BarCalculator.calculate,
}
end
end
end
The container would be accessed from my subclasses:
module MyModule
class FooCalculator
include Container::AutoInject[:foo]
def self.calculate
foo
end
end
end
module MyModule
class FooCalculator
include Container::AutoInject[:bar]
def self.calculate
bar
end
end
end
The problem is: it only works the first time. I get the error:
Dry::Container::Error - There is already an item registered with the key "foo"
I understand the container is not dying after my calculation and the next request, when I try to run the algorithm for another document, it gives me this error.
Any tips on how could I create and destroy a container instance and still be able to use dry-auto_inject
in subclasses, avoiding to pass the container as a argument to every calculation level?
Is this the correct use-case for using IoC and containers or I’m getting all this very wrong?
Thanks for the help.