Replace dependency on container

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:

# 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:

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?

One idea I have is the following.

# 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.

Seems that config.registry = ->(container, key, item, options) { container[key.to_s] = item } from dry-rb - dry-container v0.7 - Registry & Resolver also make it work without usage of the private API.

Hi. Have a look at GitHub - dry-rb/dry-effects: Algebraic effects in Ruby

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