diff --git a/src/lib/container.ts b/src/lib/container.ts index 5faed28..0f35e71 100644 --- a/src/lib/container.ts +++ b/src/lib/container.ts @@ -25,14 +25,8 @@ export class Container implements IContainer { } public register(provider: RegistrationProvider|RegistrationProvider[]): void { - provider = this.nornalizeProvider(provider); - - if (Array.isArray(provider)) { - this.registerAll( provider); - } else { - provider = this.nornalizeProvider(provider); - this.registerOne( provider); - } + const normalizedProvider = this.normalizeProvider(provider); + this.registerAll(normalizedProvider); } public resolve(token: ProviderToken): IInjectionInstance { @@ -77,11 +71,11 @@ export class Container implements IContainer { return instance; } - private registerAll(providers: RegistrationProvider[]): void { + private registerAll(providers: IProvider[]): void { providers.forEach((p: IProvider) => this.registerOne(p)); } - private registerOne(provider: IProvider) { + private registerOne(provider: IProvider): void { const registryData: IRegistryData = new RegistryData(); if (provider.useValue) { @@ -147,15 +141,10 @@ export class Container implements IContainer { } } - private nornalizeProvider(provider: RegistrationProvider|RegistrationProvider[]): IProvider { - let normalizedProvider: any; - - if (Array.isArray(provider)) { - normalizedProvider = provider.map((p: IProvider|IConstructor) => this.normalizeSingleProvider(p)); - } else { - normalizedProvider = this.normalizeSingleProvider(provider); - } - return normalizedProvider; + private normalizeProvider(provider: RegistrationProvider|RegistrationProvider[]): IProvider[] { + return Array.isArray(provider) + ? provider.map(p => this.normalizeSingleProvider(p)) + : [ this.normalizeSingleProvider(provider) ]; } private normalizeSingleProvider(provider: RegistrationProvider): IProvider { diff --git a/src/lib/interfaces.ts b/src/lib/interfaces.ts index ae4b30c..4624f13 100644 --- a/src/lib/interfaces.ts +++ b/src/lib/interfaces.ts @@ -9,11 +9,20 @@ export enum LifeTime { export interface IProvider { token: ProviderToken; - useClass?: IConstructor; + lifeTime?: LifeTime; +} + +export interface IProvider { useValue?: any; +} + +export interface IProvider { + useClass?: IConstructor; +} + +export interface IProvider { useFactory?: any; - inject?: ProviderToken[]; - lifeTime?: LifeTime; + inject?: ProviderToken[]; } export interface IInjectionMd {