I am writing a simple library using TypeScript (not using angular). I need to use a DI framework in this library. I checked out the DI framework in Angular2, but this seems like an overkill and I need to wrap my whole library in an Angular module which I don't want to do.
2 Answers
I don't use DI in TypeScript/ES6 for production (yet) because they are not yet finalized.
For experimenting, I used needlepoint which was at the time I was using ES6.
But for typescript, typescript-ioc looks like it's a better candidate. It has both IOC and DI.
Comments
Dime is a simple dependency injection library that I made. It is inspired by Angular's dependency injection system. It is still in development, so there might be some bugs. You can find more information on the wiki.
Here is a simple example of it in use:
import { ItemsService } from './items-service'; // ItemsService is an interface
import { Inject } from '@coined/dime';
class ItemsWidget {
@Inject()
private itemsService: ItemsService;
render() {
this.itemsService.getItems().subscribe(items => {
// ...
})
}
}
// Setup
const appPackage = new Package("App", {
token: "itemsService",
provideClass: AmazonItemsService // AmazonItemsService implements ItemsService
});
Dime.mountPackages(appPackage);
// Display the widget
const widget = new ItemsWidget();
widget.render();
You might have to disable strict property initializers in tsconfig.json for this to work.