DEPENDENCY INJECTIOn

USING JIMPLE

*

* Latest: v1.4.2

Oct 6, 2016

1- Simple example for everyone (incl. kids, grandfathers, cats)

2- Brief but more complex example (frameworks, big apps)

https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/1-the-mess
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/1_1-inversion-of-control
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/2-new-market
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/2_1-laptop-improvements
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/2_2-global-market-nightmare
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/2_3-improved-nightmare-with-dependency-inversion-principle
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/3-cord-as-a-dependency
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/3_1-laptop-megafactory
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/4-jimple
https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/4_1-jimple-providers

BUILDing a laptop for everyone (even for Mars COLONISTS)

- Elon Musk FTW! -

1- inversion of control (ioc)

- Before: Market > Laptop > Socket > Plug > Power On
- After: Market > Socket & Laptop > Plug > Power On

2- Dependency Inversion Principle (DIP)

- Before: Laptop > Map of Markets > Init Cord, based on Market

- After: Laptop > Cord Provider > Ask for a Cord, based on Market

3- Dependency Injection pattern

- Cord gets injected to Laptop from the outside

- constructor, setter

- Custom Cords & Sockets without altering the Laptop

4- Composition root pattern

- Everything gets wrapped in a single place

DEPENDENCY INJECTION CONTAINER

PIMPLE (PHP)

Jimple (JavaScript)

const Jimple = require('Jimple')
const Cord = require('./cord')
const Laptop = require('./laptop')

const container = new Jimple()

container.set('mars_cord', () => {
    return new Cord('MARS')
})

container.set('generic_laptop', container.factory(c => {
    return new Laptop()
}))

container.set('mars_laptop', container.factory(c => {
    const laptop = c.get('generic_laptop')
    const marsCord = c.get('mars_cord')
    laptop.attachCord(marsCord)

    return laptop
}))

https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/4-jimple

Composition root pattern

using Jimple Providers

https://github.com/sergiolepore/dependency-injection-jimple-talk/tree/4_1-jimple-providers

Anti-patterns

(Few of them)

Service Locator IN a Class

class MyClassWithDependencies
{
    constructor (container) {
        this.dep1 = container.get('dep_1')
    }
}

// VS.

class MyClassWithDependencies
{
    constructor (dep1) {
        this.dep1 = dep1
    }
}

Courier

class WithDeps
{
    constructor (myDep, depsFromMyDep) {
        this.myDep = myDep
        myDep.setDependencies(depsFromMyDep)
    }
}


// VS.

class Something
{
    constructor (somethingDeps) {
        // ...
    }
}

class WithDeps
{
    constructor (myDep) {
        // ...
    }
}

// ---

const somethingInstance = new Something(itsOwnDeps)
const withDepsInstance = new WithDeps(somethingInstance)

That's it!

Dependency Injection Pattern using Jimple

By Sergio Daniel Lepore

Dependency Injection Pattern using Jimple

  • 437

More from Sergio Daniel Lepore