# Replace dependency on container

**URL:** <https://discourse.dry-rb.org/t/replace-dependency-on-container/1270>\
**Category:** dry-system\
**Created:** [May 29, 2021, 7:17am UTC](https://discourse.dry-rb.org/t/replace-dependency-on-container/1270 "2021-05-29T07:17:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aronwolf90](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aronwolf90/32/651_2.png) [@aronwolf90](https://discourse.dry-rb.org/u/aronwolf90)\
**Post date:** [May 29, 2021, 7:17am UTC](https://discourse.dry-rb.org/t/replace-dependency-on-container/1270/1 "2021-05-29T07:17:35Z")

</div>

In the company where I work, we use a lot of rails engines, and gems. The gems and engines are the common code across customer projects. The problem we have here is that If one customer want a different behaviors than the standard, then we will overwrite the necessary code. To archive this we do something similar to the following:

```ruby
# engine/lib/concerns/create_user_service.rb
class Concerns::CreateUserService
...
end

# engine/lib/create_user_service.rb
class CreateUserService
  include Concerns::CreateUserService
end

# customer application
class CreateUserService
  include Concerns::CreateUserService

  def whatever
    ...
  end
end

```

This solution is not so nice because you will jump always with your IDE to the wrong file and also have some other minor problems. I think that one better solution would be the following:

```ruby
class Customer::CreateUserService < ::CreateUserService
    def whatever
    ...
  end
end

```

container.register(‘create\_user\_service’) {.Customer::CreateUserService }

The problem is that the service would already be registered to be the engine.

So, do you have a good idea how to handle this scenario with dry-container and dry-auto\_inject or other good suggestions?

---

<div class="post-metadata">

**Author:** ![aronwolf90](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aronwolf90/32/651_2.png) [@aronwolf90](https://discourse.dry-rb.org/u/aronwolf90)\
**Post date:** [May 29, 2021, 9:34am UTC](https://discourse.dry-rb.org/t/replace-dependency-on-container/1270/2 "2021-05-29T09:34:15Z")

</div>

One idea I have is the following.

```ruby
# engine/dependenciesConcern.rb
module WhateverEngine
  module DependenciesConcern 
    extend ActiveSupport::Concern

    included do              
      extend Dry::Container::Mixin  
   
      def self.register(key, *args)   
        _container.reject!(key.to_s)
        super
      end

      register(:action) { 'old action' }
 
      Import = Dry::AutoInject(self).kwargs
    end
  end
end

# engine/dependencies.rb
module WhateverEngine
  module Dependencies
    include WhateverEngine::DependenciesConcern
  end
end

# customer dependencies.rb
module WhateverEngine
  module Dependencies 
    include WhateverEngine::DependenciesConcern
  
    register(:action) { 'new action' }
  end
end

```

what I do not like is the usage of the private API but it works.

---

<div class="post-metadata">

**Author:** ![aronwolf90](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aronwolf90/32/651_2.png) [@aronwolf90](https://discourse.dry-rb.org/u/aronwolf90)\
**Post date:** [May 29, 2021, 9:46am UTC](https://discourse.dry-rb.org/t/replace-dependency-on-container/1270/3 "2021-05-29T09:46:04Z")

</div>

Seems that `config.registry = ->(container, key, item, options) { container[key.to_s] = item }` from [dry-rb - dry-container v0.7 - Registry & Resolver](https://dry-rb.org/gems/dry-container/0.7/registry-and-resolver/#customization) also make it work without usage of the private API.

---

<div class="post-metadata">

**Author:** ![drqCode](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/drqcode/32/223_2.png) [@drqCode](https://discourse.dry-rb.org/u/drqCode)\
**Post date:** [May 31, 2021, 11:42am UTC](https://discourse.dry-rb.org/t/replace-dependency-on-container/1270/4 "2021-05-31T11:42:39Z")

</div>

Hi. Have a look at [GitHub - dry-rb/dry-effects: Algebraic effects in Ruby](https://github.com/dry-rb/dry-effects)

---

<div class="post-metadata">

**Author:** ![aronwolf90](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/aronwolf90/32/651_2.png) [@aronwolf90](https://discourse.dry-rb.org/u/aronwolf90)\
**Post date:** [June 7, 2021, 9:27am UTC](https://discourse.dry-rb.org/t/replace-dependency-on-container/1270/5 "2021-06-07T09:27:50Z")

</div>

Nice. We will take a look. Some idea how to organize it in a multi-engine structure as we have on the company where I work? @drqCode
