Code Monkey home page Code Monkey logo

dic's Introduction

Dependency injection container explained:

I have had a hard time to understand, how does the dependency injection container design pattern work, but I think, that with the help of Peto Bodnar (https://github.com/prog) I have found my way around this pattern. Although the principle of the dependency injection was clear to me, it took me a while to address the problem of creating too many dependencies before I could create my desired object.

Imagine, we have a Car class, which has many dependencies, so in order to create a new instance of the Car class, you first need to create all of those:

$tiresFactory = new TiresFactory();
$tires = $tiresFactory->create('Michelin', 17);

$engineFactory = new EngineFactory();
$engine = $engineFactory->create();

$lightsFactory = new LightsFactory();
$lights = $lightsFactory->create();

// Here you can have many other dependencies

// Now, I can finally create my desired Car object:
$carFactory = new CarFactory();
$audiCar = $carFactory->create('Audi', 'blue', $engine, $tires, $lights);

With the help of dependency injection container, you can however do this:

$carFactory = $container->get('CarFactory');
$audiCar = $carFactory->create(CarFactory::CAR_AUDI);

Here you can see the container config file (I am using the php-di.org implementation):

return array(

    'CarFactory' => function(ContainerInterface $c) {
        $engineFactory = $c->get('EngineFactory');
        $tiresFactory = $c->get('TiresFactory');
        $lightsFactory = $c->get('LightsFactory');
        return new CarFactory($engineFactory, $tiresFactory, $lightsFactory);
    },

    'TiresFactory' => function(ContainerInterface $c) {
        return new TiresFactory();
    },

    'EngineFactory' => function(ContainerInterface $c) {
        return new EngineFactory();
    },

    'LightsFactory' => function(ContainerInterface $c) {
        return new LightsFactory();
    }

);

As you can see from the code above, what we do in the container is, we create a 'service factory' and we return that factory from the container. Then, in your client code, you can create an instance of the object by calling the 'create()' factory method. In case the 'service' (factory) depends on an other service, you inject that service into the other one.

Some important things to notice:

  • Always return a factory from the container
  • You should inject only other factories into a factory as a dependency
  • Distinguish between 'product dependencies' (the result of the create() factory method) and 'service dependencies'. Service dependencies should be injected in the container, while 'product dependencies' should be injected directly in the constructor of the desired object you want to create.

Notice, that I use 3 factories: 'engineFactory', 'tiresFactory', 'lightsFactory' ... Since those are 'services', I create them in the service container and I pass those services into the CarFactory:

return [
    'CarFactory' => function(ContainerInterface $c) {
        $engineFactory = $c->get('EngineFactory');
        $tiresFactory = $c->get('TiresFactory');
        $lightsFactory = $c->get('LightsFactory');
        return new CarFactory($engineFactory, $tiresFactory, $lightsFactory);
    }
]

In the carFactory, I can then create a Car object and I can leverage the other factories (services) like so:

$engine = $this->engineFactory->create();
$tires = $this->tiresFactory->create('Goodyear', 17);
$lights = $this->lightsFactory->create();
return new Car('Audi', 'blue', $engine, $tires, $lights);

Check the project source code https://github.com/durino13/dic for more info.

dic's People

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.