Skip to content

Commit 29b765a

Browse files
Merge pull request #101 from Tolowe/feature/70-self-inject-container
feat(container): add self registration of container in constructor, t…
2 parents 29d290a + 869bada commit 29b765a

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Container, Injectable } from 'container-ioc';
2+
3+
const container = new Container();
4+
5+
@Injectable([Container])
6+
class Builder {
7+
constructor(container) {}
8+
}
9+
10+
container.register({ token: Builder, useClass: Builder });
11+
12+
const builder = container.resolve(Builder);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { IContainer, Container, Injectable, Inject } from 'container-ioc';
2+
3+
const container = new Container();
4+
5+
@Injectable()
6+
class Builder {
7+
constructor(@Inject(Container) private container: IContainer) {}
8+
}
9+
10+
container.register({ token: Builder, useClass: Builder });
11+
12+
const builder = container.resolve(Builder);

src/lib/container.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface IContainerOptions {
55
defaultLifeTime?: LifeTime;
66
}
77

8-
export interface IContainer {
8+
export interface IContainer {
99
register(provider: RegistrationProvider|RegistrationProvider[]): void;
1010

1111
resolve(token: ProviderToken): IInjectionInstance;

src/lib/container.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class Container implements IContainer {
2121
this.parent = <IContainer> options.parent;
2222
this.defaultLifeTime = options.defaultLifeTime || this.defaultLifeTime;
2323
}
24+
this.register({ token: Container, useValue: this });
2425
}
2526

2627
public register(provider: RegistrationProvider|RegistrationProvider[]): void {

src/tests/container.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,20 @@ describe('Container', () => {
356356

357357
expect(throwableFunc).to.throw('No provider for IB. Trace: IA --> IB');
358358
});
359+
360+
it('should resolve container instance when injected into class Literal', () => {
361+
@Injectable()
362+
class TestClass {
363+
constructor(@Inject(Container) public a: IContainer) {}
364+
}
365+
366+
container.register({ token: TestClass, useClass: TestClass });
367+
const actual = container.resolve(TestClass);
368+
369+
expect(actual).to.be.ok;
370+
expect(actual.a).to.be.ok;
371+
expect(actual.a).to.equal(container);
372+
});
359373
});
360374

361375
describe('Hierarchial', () => {
@@ -449,6 +463,13 @@ describe('Container', () => {
449463

450464
expect(instance1).not.to.be.equal(instance2);
451465
});
466+
467+
it('should register itself for injection', () => {
468+
const actual = container.resolve(Container);
469+
470+
expect(actual).to.be.ok;
471+
expect(actual).to.equal(container);
472+
});
452473
});
453474

454475
describe('createChild()', () => {
@@ -471,5 +492,5 @@ describe('Container', () => {
471492

472493
expect(value).to.be.equal('string');
473494
});
474-
});
495+
});
475496
});

0 commit comments

Comments
 (0)