Saturday, December 30, 2017

Dependency Injection

What is Dependency Injection

A technique of 'injecting' the dependencies of one object to another.

Not this:
function a (id) {
         return b(id)
                       .then(response => response.json())
}

This:
function a (b, id) {
          return b(id)
                       .then(response => response.json())
}

Why use it?

It's more explicit, easier to read and provides more control over inputs...but practically speaking, it makes it easier to unit test your application. There are many ways to test your application, but dependency injection is a common and well known design pattern, well documented and can (for the most part) be implemented with most languages.

Source
https://www.youtube.com/watch?v=0X1Ns2NRfks
https://en.wikipedia.org/wiki/Dependency_injection

No comments:

Post a Comment