Skip to content

Commit 3283b81

Browse files
vsavkingoderbauer
authored andcommitted
feat(di): added resolveAndInstantiate and instantiateResolved to Injector
These two methods can be used to create objects in the context of the injector without storing them in the injector.
1 parent b97f1bf commit 3283b81

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

modules/angular2/src/di/binding.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,7 @@ function _extractToken(typeOrFunc, metadata /*List<any> | any*/, params: List<Li
400400
}
401401

402402
var lowerBoundVisibility = null;
403-
;
404403
var upperBoundVisibility = null;
405-
;
406404

407405
for (var i = 0; i < metadata.length; ++i) {
408406
var paramMetadata = metadata[i];

modules/angular2/src/di/injector.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,34 @@ export class Injector {
552552
return inj;
553553
}
554554

555+
/**
556+
* Resolves a binding and instantiates an object in the context of the injector.
557+
*
558+
* @param `binding`: either a type or a binding.
559+
* @returns an object created using binding.
560+
*/
561+
resolveAndInstantiate(binding: Type | Binding) {
562+
return this.instantiateResolved(Injector.resolve([binding])[0]);
563+
}
564+
565+
/**
566+
* Instantiates an object using a resolved bindin in the context of the injector.
567+
*
568+
* @param `binding`: a resolved binding
569+
* @returns an object created using binding.
570+
*/
571+
instantiateResolved(binding: ResolvedBinding): any {
572+
return this._instantiate(binding, PUBLIC_AND_PRIVATE);
573+
}
574+
555575
_new(binding: ResolvedBinding, visibility: number): any {
556576
if (this._constructionCounter++ > this._strategy.getMaxNumberOfObjects()) {
557577
throw new CyclicDependencyError(this, binding.key);
558578
}
579+
return this._instantiate(binding, visibility);
580+
}
559581

582+
private _instantiate(binding: ResolvedBinding, visibility: number): any {
560583
var factory = binding.factory;
561584
var deps = binding.dependencies;
562585
var length = deps.length;

modules/angular2/test/di/injector_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,29 @@ export function main() {
395395
});
396396
});
397397

398+
describe('resolveAndInstantiate', () => {
399+
it('should instantiate an object in the context of the injector', () => {
400+
var inj = Injector.resolveAndCreate([Engine]);
401+
var car = inj.resolveAndInstantiate(Car);
402+
expect(car).toBeAnInstanceOf(Car);
403+
expect(car.engine).toBe(inj.get(Engine));
404+
});
405+
406+
it('should not store the instantiated object in the injector', () => {
407+
var inj = Injector.resolveAndCreate([Engine]);
408+
inj.resolveAndInstantiate(Car);
409+
expect(() => inj.get(Car)).toThrowError();
410+
});
411+
});
412+
413+
describe('instantiate', () => {
414+
it('should instantiate an object in the context of the injector', () => {
415+
var inj = Injector.resolveAndCreate([Engine]);
416+
var car = inj.instantiateResolved(Injector.resolve([Car])[0]);
417+
expect(car).toBeAnInstanceOf(Car);
418+
expect(car.engine).toBe(inj.get(Engine));
419+
});
420+
});
398421

399422
describe("depedency resolution", () => {
400423
describe("@Self()", () => {

0 commit comments

Comments
 (0)