3

I've been looking for a DI framework that works well across TypeScript and pure JavaScript (es5). I've come across some implementations, such as Inversify and Angular's 2 DI. However, Inversify seems to be based around Ninject, which makes a few weird assumptions such as instance scope instead of singleton by default, which seems to contradict what many other DI frameworks do by default. Angular's 2 DI is deeply coupled with the framework, and there is no way of easily consuming it from outside, specially in Node apps.

I was wondering if there other options out there.

0

2 Answers 2

2

I'm the author of bottlejs. Bottle is written for ES5 compliant environments and there is a TypeScript declaration file included in the distribution.

Bottle's API was originally inspired by the Angular 1.x API, so if you're familiar with that you can get up and running pretty quickly. Defined services are singletons and we support dot notated namespaces to deal with the problem of collisions.

1

I'm the author of InversifyJS. A singleton is like an static class, in general you don't want to flag all your classes as static because it can lead to memory problems. Using transient scope by default is a better practice.

Using singletons has been identified as one of the main problems of the injector in Angular 1.x applications:

This is all cool but it turns out, that the existing DI has some problem though:

  • Internal cache - Dependencies are served as singletons. Whenever we ask for a service, it is created only once per application lifecycle. Creating factory machinery is quite hairy.

  • Namespace collision - There can only be one token of a “type” in an application. If we have a car service, and there’s a third-party extension that also introduces a service with the same name, we have a problem.

  • Built into the framework - Angular 1’s DI is baked right into the framework. There’s no way for us to use it decoupled as a standalone system.

Both InversifyJS and the angular 2.x injector solves these problems. If you want to ask more questions and suggest improvements you can do so at gitter.

Other options are:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.