Skip to content

feat(container): add self registration of container in constructor, t… #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/javascript/resolving-container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Container, Injectable } from 'container-ioc';

const container = new Container();

@Injectable([Container])
class Builder {
constructor(container) {}
}

container.register({ token: Builder, useClass: Builder });

const builder = container.resolve(Builder);
12 changes: 12 additions & 0 deletions examples/typescript/resolving-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IContainer, Container, Injectable, Inject } from 'container-ioc';

const container = new Container();

@Injectable()
class Builder {
constructor(@Inject(Container) private container: IContainer) {}
}

container.register({ token: Builder, useClass: Builder });

const builder = container.resolve(Builder);
2 changes: 1 addition & 1 deletion src/lib/container.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface IContainerOptions {
defaultLifeTime?: LifeTime;
}

export interface IContainer {
export interface IContainer {
register(provider: RegistrationProvider|RegistrationProvider[]): void;

resolve(token: ProviderToken): IInjectionInstance;
Expand Down
1 change: 1 addition & 0 deletions src/lib/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Container implements IContainer {
this.parent = <IContainer> options.parent;
this.defaultLifeTime = options.defaultLifeTime || this.defaultLifeTime;
}
this.register({ token: Container, useValue: this });
}

public register(provider: RegistrationProvider|RegistrationProvider[]): void {
Expand Down
23 changes: 22 additions & 1 deletion src/tests/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,20 @@ describe('Container', () => {

expect(throwableFunc).to.throw('No provider for IB. Trace: IA --> IB');
});

it('should resolve container instance when injected into class Literal', () => {
@Injectable()
class TestClass {
constructor(@Inject(Container) public a: IContainer) {}
}

container.register({ token: TestClass, useClass: TestClass });
const actual = container.resolve(TestClass);

expect(actual).to.be.ok;
expect(actual.a).to.be.ok;
expect(actual.a).to.equal(container);
});
});

describe('Hierarchial', () => {
Expand Down Expand Up @@ -449,6 +463,13 @@ describe('Container', () => {

expect(instance1).not.to.be.equal(instance2);
});

it('should register itself for injection', () => {
const actual = container.resolve(Container);

expect(actual).to.be.ok;
expect(actual).to.equal(container);
});
});

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

expect(value).to.be.equal('string');
});
});
});
});