more containers derived from dry-container

Gem Version Build Status Dependency Status Code Climate Coverage Status Issue Count


gem install dry-dependency-injection

or Gemfile:

gem 'dry-dependency-injection'

Singleton Container with Dependency Injection

The idea is to have components or services which all are using dry-auto_inject to inject their dependencies. The Dry::More::Container::Singleton is dry-container with a special resovler. You need register the service/component class itself (not the an instance of it):

class Singletons; extend Dry::DependencyInjection::Singletons; end
Import = Dry::AutoInject(Singletons)

class A; include Import['b']; end
class B; include Import['c']; end
class C; include Import['d']; end
class D; end

singletons.register('a', A)
singletons.register('b', B)
singletons.register('c', C)
singletons.register('d', D)

On retrieve the singleton container will create a single instance of the class under the given key and also resolves the dependencies as well in the same manner:


Now the singleton container has an instance of all services/components.

On circular dependencies there will be an Dry::Container::Error.

See also singletons_spec.

Finalize, Lazy vs. Eager

The container is instantiating the components per default in a lazy way. The finalize method on the container does ensure all eager components get initialized. If you configure the container to be not-lazy then the finalize will instantiate all components. If the container is lazy then finalize only instantiate the components which are marks as eager by extending the component with Dry::DependencyInjection::Eager

class MyEagerComponent
  extend Dry::DependencyInjection::Eager

In case there are no eager component then finalize is noop. The eager loading must be first configured and then the finalize method needs to be called:

container.config.lazy = false

Plugin Example

Please see the simple plugin implementation in example provided.


Bug reports, comments and pull requests are welcome.


be happy and enjoy.