-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 1 commit
82f016a
52ee7d4
bfaf469
869bada
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); |
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); |
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'; | ||
|
@@ -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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
public register(provider: RegistrationProvider|RegistrationProvider[]): void { | ||
|
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'; | ||
|
@@ -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', () => { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()', () => { | ||
|
@@ -471,5 +494,5 @@ describe('Container', () => { | |
|
||
expect(value).to.be.equal('string'); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.