No commit activity in last 3 years
No release in over 3 years
add dependency injection to dry-container
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 12.0
~> 3.4

Runtime

 Project Readme

dry-dependency-injection

more containers derived from dry-container

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

Rubygems/Bundler

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:

Singletons['a']

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
container.finalize

Plugin Example

Please see the simple plugin implementation in example provided.

Contributing

Bug reports, comments and pull requests are welcome.

Meta-Foo

be happy and enjoy.