2

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.

1

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.