Cover image by Papiertrümmer on Flickr
Why?
We all write code that depends on other code, this is completely normal. Even if we don't ...
For further actions, you may consider blocking this person and/or reporting abuse
To put my two penny worth into this, I think one underrated benefit from this approach is that the code becomes more testable out of the box. If you think about component mocking, it is suddenly super easy to mock an object and inject it like nothing happened. :) But that is in general, not only JavaScript related.
This is in fact the reason a lot of Javascript code is notoriously hard to test. You basically need a complete browser to test even the most trivial stuff. Testability and good design go hand in hand. Anything that can be injected, can trivially be mocked. Once everything not core to the unit you are testing is injectable, the thing is highly testable.
The approach above is what is known as DYI dependency injection in the Java world. It's totally valid as DI is just a design pattern. But not very common because it does result in a lot of boiler plate code and it's a bit of a PITA to manually wire things up (not to mention error prone).
In the Java world, frameworks such as Spring provide a lot of DI infrastructure that has evolved to require less and less boiler plate. Combined with modern languages such as Kotlin, there is now a lot less need for stating the obvious than ever before.
Simply having a class with a constructor that takes arguments and a single annotation tells Spring "This is a bean, please construct an instance and pass it to places where it is needed as a dependency. It needs other beans to work as well. The names of those beans are the constructor arguments". If you then define those other beans in the same way, Spring takes care of all the plumbing, new calls to constructors of all the beans it has identified, configuration injection (with different profiles, default values, override mechanisms, etc), bean graph validation (no cycles, everything needs to resolve, etc) and then fires the whole thing up.
With stuff like Spring Boot, there is almost no code involved at all and you get a lot of things self initializing using sane defaults simply because the library is on the class path. I've coached a few frontend engineers dealing with some Spring Boot code. I wouldn't go as far as to say that they like it but they were all able to get productive pretty quickly.
Something like Spring is missing in the Javascript world and given the sorry state a lot of js projects end up in, it is actually needed. I see why code such as above is necessary. It's clearly better than the traditional spaghetti code approach (which, lets face it, is the common alternative). But it still looks like a lot of boiler plate to me to write, maintaint, test, and debug. And you don't even get typesafety with that. This just begs some decent automation and a bit more systematic approach to enforcing sane architecture. You get bits and pieces of that if you use react or similar frameworks but it requires a lot of discipline and expertise to stay on top of things.
I recently discovered that I can use ES6 default value for parameters to by default use the method that has the proper implementation, and injecting something else when needed. It goes at the end of the list of parameters, so it doesn't need to be provided.
Does anybody see any possible problem with that approach?
Could you provide an example?
Lets say you have this amazing util function that looks up some data in some static/global read only Map and does some calculation that you need in many places in your code base, lets imagine the following signature:
function wow(number): Metric
.In some other function
foo
in a different module I'm doing the following to be able to use it easily, but still be able to pass a mock or stub when I want to unit-testfoo
without invokingwow
.In TypeScript this could look like this:
Is this helpful?
Ah, got it.
I probably would have implemented with a curried function.
in the regular place I'd use
and for testing something different, but I guess your way works too, it's just a bit more implicit.
To be annoyingly pedantic, createFoo above is not a curried function, because it can't be called like createFoo(wow, data) and like createFoo(wow, data) at the same time, it's a higher order function with aurity of 1
I allow it!
Thanks for this good article.
I coded my own JavaScript Dependency Injection Framework called Di-Ninja
github.com/di-ninja/di-ninja
It's full featured and is currently the only one in javascript, as I know, that implement the awesome Composition-Root design pattern,
helping you to keep all things decoupled and to wire application components and config at one unique root place.
blog.ploeh.dk/2011/07/28/Compositi...
It work well with NodeJS and Webpack
Any feedback would be appreciated
Hey Jo, di-ninja looks very promising. Curious as to why you decided to code your own vs. bottlejs, InversifyJS, etc. out there?
Hey Jonathan,
thanks for your interest,
bottlejs is just an enhanced registry design pattern used as a factory and service locator, and InversifyJS provide only one of the two approach offered by Di-Ninja: the decorator approach. This approach, like the implicit strategy behind bottlejs and many others DiC, encourage coupling between the components of your application and the DiC library itself, and, only for the decorator approach, the decoupling between component rely on arbitrary interface or name.
Di-Ninja is the only one that offer Composition-Root design pattern, allowing
Another point is that InversifyJS require TypeScript, Di-Ninja can work with TypeScript, Babel (my preferred), and even without an application level transpiller, and it work fine on NodeJs, Browser (tested with webpack) and recently with React-Native environnement.
And there is many other features offered by Di-Ninja in the context of Composition-Root design pattern paradigm, but I will not list them all here, take a look on full documentation github.com/di-ninja/di-ninja to discover them ;) .