With TypeScript now we have static analyze and many OOP features in JavaScript. So it's also time to have better unit tests in client side logic and as well we need IOC container for dependency injections to make code more testable...
So, have someone already experienced it this subject or maybe know libraries for typescript or JavaScript frameworks that can be porting to TypeScript?
Alternatively you can just use no framework and use class as container with object factories as properties. You can then inherit this class in tests and change factories. This approach is type safe and do not require any decorators, just registration of classes.
For now, you can use dependency injection in JavaScript without the IOC part. It is up to you whether you write a "manual" resolver, or factories, or whatever DI pattern you prefer.
When the ECMAScript 6 standard is adopted, it may make the concept of IOC possible in JavaScript.
checkout https://github.com/typestack/typedi
something like this is possible:
I have created DI library for typescript - huject
https://github.com/asvetliakov/huject
Example:
There is a problem with TypeScript interfaces though, but i have 2 workarounds (use abstract or simple class as interface)
I have developed an IoC container called InversifyJS with advanced dependency injection features like contextual bindings.
You need to follow 3 basic steps to use it:
1. Add annotations
The annotation API is based on Angular 2.0:
2. Declare bindings
The binding API is based on Ninject:
3. Resolve dependencies
The resolution API is based on Ninject:
The latest release (2.0.0) supports many use cases:
You can learn more about it at https://github.com/inversify/InversifyJS
We've been using a simple dependency injection container which uses AMD define/require - like syntax. The original implementation is in TypeScript, although the blog post below presents it in plain old JavaScript.
http://blog.coolmuse.com/2012/11/11/a-simple-javascript-dependency-injection-container/
It's pretty straightforward to define dependency relationships without requiring a bunch of configuration, and supports circular dependency resolution similar to requirejs.
Here's a simple example: