ReflectiveInjector

class deprecated

npm Package @angular/core
Module import { ReflectiveInjector } from '@angular/core';
Source core/src/di/reflective_injector.ts

Deprecation Notes

from v5 - slow and brings in a lot of code, Use Injector.create instead.

Overview

class ReflectiveInjector implements Injector {
  static resolve(providers: Provider[]): ResolvedReflectiveProvider[]
  static resolveAndCreate(providers: Provider[], parent?: Injector): ReflectiveInjector
  static fromResolvedProviders(providers: ResolvedReflectiveProvider[], parent?: Injector): ReflectiveInjector
  get parent: Injector | null
  resolveAndCreateChild(providers: Provider[]): ReflectiveInjector
  createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector
  resolveAndInstantiate(provider: Provider): any
  instantiateResolved(provider: ResolvedReflectiveProvider): any
  get(token: any, notFoundValue?: any): any
}

Description

A ReflectiveDependency injection container used for instantiating objects and resolving dependencies.

An Injector is a replacement for a new operator, which can automatically resolve the constructor dependencies.

In typical use, application code asks for the dependencies in the constructor and they are resolved by the Injector.

Example (live demo)

The following example creates an Injector configured to create Engine and Car.

@Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);
var car = injector.get(Car);
expect(car instanceof Car).toBe(true);
expect(car.engine instanceof Engine).toBe(true);

Notice, we don't use the new operator because we explicitly want to have the Injector resolve all of the object's dependencies automatically.

Static Members

static resolve(providers: Provider[]): ResolvedReflectiveProvider[]

Turns an array of provider definitions into an array of resolved providers.

A resolution is a process of flattening multiple nested arrays and converting individual providers into an array of ResolvedReflectiveProviders.

Example (live demo)

@Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var providers = ReflectiveInjector.resolve([Car, [[Engine]]]);

expect(providers.length).toEqual(2);

expect(providers[0] instanceof ResolvedReflectiveProvider).toBe(true);
expect(providers[0].key.displayName).toBe("Car");
expect(providers[0].dependencies.length).toEqual(1);
expect(providers[0].factory).toBeDefined();

expect(providers[1].key.displayName).toBe("Engine");
});

See fromResolvedProviders for more info.

static resolveAndCreate(providers: Provider[], parent?: Injector): ReflectiveInjector

Resolves an array of providers and creates an injector from those providers.

The passed-in providers can be an array of Type, Provider, or a recursive array of more providers.

Example (live demo)

@Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);
expect(injector.get(Car) instanceof Car).toBe(true);

This function is slower than the corresponding fromResolvedProviders because it needs to resolve the passed-in providers first. See resolve and fromResolvedProviders.

static fromResolvedProviders(providers: ResolvedReflectiveProvider[], parent?: Injector): ReflectiveInjector

Creates an injector from previously resolved providers.

This API is the recommended way to construct injectors in performance-sensitive parts.

Example (live demo)

@Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var providers = ReflectiveInjector.resolve([Car, Engine]);
var injector = ReflectiveInjector.fromResolvedProviders(providers);
expect(injector.get(Car) instanceof Car).toBe(true);

Members

get parent: Injector | null

Parent of this injector.

Example (live demo)

var parent = ReflectiveInjector.resolveAndCreate([]);
var child = parent.resolveAndCreateChild([]);
expect(child.parent).toBe(parent);

resolveAndCreateChild(providers: Provider[]): ReflectiveInjector

Resolves an array of providers and creates a child injector from those providers.

The passed-in providers can be an array of Type, Provider, or a recursive array of more providers.

Example (live demo)

class ParentProvider {}
class ChildProvider {}

var parent = ReflectiveInjector.resolveAndCreate([ParentProvider]);
var child = parent.resolveAndCreateChild([ChildProvider]);

expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true);
expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true);
expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));

This function is slower than the corresponding createChildFromResolved because it needs to resolve the passed-in providers first. See resolve and createChildFromResolved.

createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector

Creates a child injector from previously resolved providers.

This API is the recommended way to construct injectors in performance-sensitive parts.

Example (live demo)

class ParentProvider {}
class ChildProvider {}

var parentProviders = ReflectiveInjector.resolve([ParentProvider]);
var childProviders = ReflectiveInjector.resolve([ChildProvider]);

var parent = ReflectiveInjector.fromResolvedProviders(parentProviders);
var child = parent.createChildFromResolved(childProviders);

expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true);
expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true);
expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));

resolveAndInstantiate(provider: Provider): any

Resolves a provider and instantiates an object in the context of the injector.

The created object does not get cached by the injector.

Example (live demo)

@Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Engine]);

var car = injector.resolveAndInstantiate(Car);
expect(car.engine).toBe(injector.get(Engine));
expect(car).not.toBe(injector.resolveAndInstantiate(Car));

instantiateResolved(provider: ResolvedReflectiveProvider): any

Instantiates an object using a resolved provider in the context of the injector.

The created object does not get cached by the injector.

Example (live demo)

@Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Engine]);
var carProvider = ReflectiveInjector.resolve([Car])[0];
var car = injector.instantiateResolved(carProvider);
expect(car.engine).toBe(injector.get(Engine));
expect(car).not.toBe(injector.instantiateResolved(carProvider));

get(token: any, notFoundValue?: any): any

© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v5.angular.io/api/core/ReflectiveInjector