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 1 commit
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, TContainer, Injectable } from 'container-ioc';

const container = new Container();

@Injectable([TContainer])
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, TContainer, Injectable, Inject } from 'container-ioc';

const container = new Container();

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

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

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

export interface IContainer {
[key: string]: any;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing that it's a bad practise. Interface (aka super type) should define only public methods. Unless you use interface for defining structure of value objects. Please remove that.


register(provider: RegistrationProvider|RegistrationProvider[]): void;

resolve(token: ProviderToken): IInjectionInstance;
Expand All @@ -18,4 +20,6 @@ export interface IContainer {
createChild(): IContainer;

setParent(parent: IContainer): void;
}
}

export const TContainer = Symbol('IContainer');
3 changes: 2 additions & 1 deletion src/lib/container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IConstructor, IInjectionInstance, IInjectionMd, IProvider, LifeTime, ProviderToken, RegistrationProvider } from './interfaces';
import { FactoryFunction, IFactory, IRegistryData, RegistryData } from './registry-data';
import { IContainer, IContainerOptions } from './container.interface';
import { IContainer, IContainerOptions, TContainer } from './container.interface';
import { ClassNotInjectableError, InvalidProviderProvidedError, NoProviderError } from './exceptions';
import { INJECTABLE_MD_KEY, INJECTIONS_MD_KEY } from './metadata/keys';
import { IMetadataAnnotator } from './metadata/metadata-annotator.interface';
Expand All @@ -21,6 +21,7 @@ export class Container implements IContainer {
this.parent = <IContainer> options.parent;
this.defaultLifeTime = options.defaultLifeTime || this.defaultLifeTime;
}
this.register({ token: TContainer, useValue: this });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did right! But I'm gonna release that simplified Reflect API which is gonna allow us to inject stuff by just specifying it's type like this constructor (private container: Container) {}. Please use Container as the token in this case.

}

public register(provider: RegistrationProvider|RegistrationProvider[]): void {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { Container } from './container';
export { IContainer } from './container.interface';
export { IContainer, TContainer } from './container.interface';
export { Inject, Injectable } from './decorators';
export { InjectionToken } from './injection-token';
export { IMetadataAnnotator } from './metadata/metadata-annotator.interface';
Expand Down
27 changes: 25 additions & 2 deletions src/tests/container.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IContainer } from '../lib/container.interface';
import { IContainer, TContainer } from '../lib/container.interface';
import { Container } from '../lib/index';

import 'mocha';
Expand Down Expand Up @@ -356,6 +356,21 @@ describe('Container', () => {

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

it('should resolve container instance when injected into class Literal', () => {
// arrange
@Injectable()
class TestClass {
constructor(@Inject(TContainer) public a: IContainer) {}
}
container.register({ token: TestClass, useClass: TestClass });
// act
const actual = container.resolve(TestClass);
// assert
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 +464,14 @@ describe('Container', () => {

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

it('should register itself for injection', () => {
// arrange, act
const actual = container.resolve(TContainer);
// assert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't deny that comments like this one are obvious. Clearly named variables and methods won't need any comments above them. Please remove them.

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

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

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