# Approach on using dry-container and dry-auto\_inject on a Rails app

**URL:** <https://discourse.dry-rb.org/t/approach-on-using-dry-container-and-dry-auto-inject-on-a-rails-app/689>\
**Category:** Support\
**Created:** [January 8, 2019, 2:08pm UTC](https://discourse.dry-rb.org/t/approach-on-using-dry-container-and-dry-auto-inject-on-a-rails-app/689 "2019-01-08T14:08:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![taschetto](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/taschetto/32/330_2.png) [@taschetto](https://discourse.dry-rb.org/u/taschetto)\
**Post date:** [January 8, 2019, 2:08pm UTC](https://discourse.dry-rb.org/t/approach-on-using-dry-container-and-dry-auto-inject-on-a-rails-app/689/1 "2019-01-08T14:08:12Z")

</div>

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:

```auto
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:

```auto
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:

```auto
module MyModule
  class FooCalculator
    include Container::AutoInject[:foo]

    def self.calculate
      foo
    end
  end
end

```

```auto
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. 🙂

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [January 9, 2019, 9:15pm UTC](https://discourse.dry-rb.org/t/approach-on-using-dry-container-and-dry-auto-inject-on-a-rails-app/689/2 "2019-01-09T21:15:44Z")

</div>

Hi @taschetto, I think you’re right in your hunch that this isn’t a typical usage of a container. Even if you solve the issue of emptying the container before you re-register things (and to do this you’d need to mess around with the container’s internal `_container` hash, since the public API doesn’t support re-registering), I think there would have to be a better, more fitting solution lying around.

If I were approaching this, I’d go the manual route of passing every argument to every method in your tree of objects, and then work backwards from there to see if things can be made nicer to handle (e.g. maybe creating another object to hold the various args together in a clump?). A good solution would depend a lot on the particulars of your problem, but my feeling is that it would probably never involve an IoC container. I’d mostly try and work with building custom classes and objects.
