DEV Community

Discussion on: Dependency Injection in JavaScript

Collapse
 
ernsheong profile image
Jonathan ES Lin • Edited

Hey Jo, di-ninja looks very promising. Curious as to why you decided to code your own vs. bottlejs, InversifyJS, etc. out there?

Collapse
 
devthejo profile image
Jo

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

  • full decoupling between component: all exported components are factories that will only expect parameters
  • full decoupling from DiC: that means you can totally implement Di-Ninja as a top overlay without needing to rewrite any of your components (off course you'll have to remove singletons anti-pattern and replace them with factories if your goal is to make a clean design), your components doesn't need to know anything about Di-Ninja, only expect their own dependencies as parameters variables, and you can remove totally Di-Ninja and replace it with a raw Composition-Root, you'll have more code and it will be less readable but you can preserve the top overlay architecture, so Di-Ninja encourages you to best-practice by a REAL Inversion of Control paradigm

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 ;) .