Skip to content

Instantly share code, notes, and snippets.

@korniychuk
Created November 11, 2016 20:24
Show Gist options
  • Save korniychuk/baf60c5124458de2939a301ecd347bfe to your computer and use it in GitHub Desktop.
Save korniychuk/baf60c5124458de2939a301ecd347bfe to your computer and use it in GitHub Desktop.
Dependency container example :: typescript
declare type Dependency = any;
declare type DependencyKey = string|number|{new(...args: any[])};
/**
* Dependency injection container
*
* Example:
*
* // There is a class
* class Auth { ... }
*
* let di = new Di(); // Makes a container
* di.set(Auth); // Add the dependency
* global['appDi'] = di; // Publish container
*
* // Get instance from Di in another place
* let auth: Auth = appDi.get(Auth);
* // or
* let auth: Auth = appDi.get('Auth');
*
*/
export class Di {
private _container = new Map<DependencyKey, Dependency>();
/**
* Add a dependency to the container
*
* Example 1:
*
* di.set('someName', 'example string value');
* di.set('App', App);
* di.set('config', {apiUrl : 'http://...'});
*
* Example 2:
*
* // The class will be used also as key
* di.set(App, App);
* di.set(ModuleManager);
*
*/
public set(key: DependencyKey, value?: Dependency): void {
if (key instanceof Function) {
value = key;
key = this.getClassName(key);
}
if (value instanceof Function) {
this._container.set(key, new value());
} else if (value) {
this._container.set(key, value);
} else {
throw new Error(`Di: Dependency entity should be a true value. Actual: ${typeof value}`);
}
} // end set();
public get(key: DependencyKey): Dependency {
if (key instanceof Function) {
key = this.getClassName(key);
}
return this._container.get(key);
}
/**
* @param key
* @return {boolean} is deleted successful
*/
public remove(key: DependencyKey): boolean {
return this._container.delete(key);
}
public has(key: DependencyKey): boolean {
return this._container.has(key);
}
private getClassName(Class: {new(...args: any[])}): string {
return Class.name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment