decoration-ioc
decoration-ioc copied to clipboard
🔌The simple, type-safe dependency injection system used in Visual Studio Code.
Visual Studio Code's "instantiation" system
decoration-ioc is a very simple inversion of control and dependency injection framework. Taken directly from Visual Studio Code, decoration-ioc provides robust service location and constructor injection with automatic dependency resolution.
Quickstart
First, make sure that the experimentalDecorators
TypeScript compiler option is set to true
.
Define a service interface
// Define the service.
export interface IMyService {
_serviceBrand: undefined;
sayHello(): string;
}
// Create a decorator used to reference the interface type.
export const IMyService = createDecorator<IMyService>('myService');
Note that the decorator has the same name as the interface. You'll only need to import IMyService
!
Create a concrete implementation
export class MyService implements IMyService {
_serviceBrand: undefined;
sayHello() {
return 'Hello!';
}
}
Register the concrete type for the service
// Create a service collection where concrete implementation types are registered.
const serviceCollection = new ServiceCollection();
// Declare that the MyService class is the type that is instantiated when an IMyService is needed.
serviceCollection.set(IMyService, new Descriptor(MyService));
Creating instances
// Create an instantiation service that performs constructor injection.
// It uses the service collection to resolve dependencies and create instances.
const instantiationService = new InstantiationService(serviceCollection);
// This is a class that requires an instance of IMyService when created.
export class MyDependentClass {
private _myService: IMyService;
// The myService parameter is annotated with the IMyService decorator.
constructor(@IMyService myService: IMyService) {
this._myService = myService;
}
makeMyServiceSayHello() {
console.log(this._myService.sayHello());
}
}
// Create an instance of myDependentClass.
const myDependentClass = instantiationService.createInstance(MyDependentClass);
myDependentClass.makeMyServiceSayHello();