Skip to content

Commit e0185e5

Browse files
Merge pull request thiagobustamante#26 from nolazybits/master
adding the getType method
2 parents 4581803 + 4a40418 commit e0185e5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/typescript-ioc.ts

+18
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ export class Container {
247247
return IoCContainer.get(source);
248248
}
249249

250+
/**
251+
* Retrieve a type associated with the type provided from the container
252+
* @param source The dependency type to resolve
253+
* @return an object resolved for the given source type;
254+
*/
255+
static getType(source: Function) {
256+
return IoCContainer.getType(source);
257+
}
258+
250259
/**
251260
* Store the state for a specified binding. Can then be restored later. Useful for testing.
252261
* @param source The dependency type
@@ -308,6 +317,13 @@ class IoCContainer {
308317
return config.getInstance();
309318
}
310319

320+
static getType(source: Function): Function {
321+
checkType(source);
322+
const baseSource = InjectorHanlder.getConstructorFromType(source);
323+
const config: ConfigImpl = IoCContainer.bindings.get(baseSource);
324+
return config.targetSource || config.source;
325+
}
326+
311327
static injectProperty(target: Function, key: string, propertyType: Function) {
312328
const propKey = `__${key}`;
313329
Object.defineProperty(target.prototype, key, {
@@ -368,6 +384,7 @@ export interface Config {
368384

369385
class ConfigImpl implements Config {
370386
source: Function;
387+
targetSource: Function;
371388
iocprovider: Provider;
372389
iocscope: Scope;
373390
decoratedConstructor: FunctionConstructor;
@@ -380,6 +397,7 @@ class ConfigImpl implements Config {
380397
to(target: FunctionConstructor) {
381398
checkType(target);
382399
const targetSource = InjectorHanlder.getConstructorFromType(target);
400+
this.targetSource = targetSource;
383401
if (this.source === targetSource) {
384402
const configImpl = this;
385403
this.iocprovider = {

test/unit/test.spec.ts

+29
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,35 @@ describe("The IoC Container.get(source)", () => {
316316
});
317317
});
318318

319+
describe("The IoC Container.getType(source)", () => {
320+
321+
abstract class ITest {
322+
public abstract testValue: string;
323+
}
324+
325+
class Test implements ITest {
326+
public testValue: string = "success";
327+
}
328+
329+
330+
class TestNoProvider {
331+
public testValue: string = "success";
332+
}
333+
334+
IoC.Container.bind(ITest).to(Test);
335+
IoC.Container.bind(TestNoProvider);
336+
337+
it("should retrieve type used by the Container", () => {
338+
const clazz: Function = IoC.Container.getType(ITest);
339+
expect(clazz).to.be.equal(Test);
340+
341+
const clazzNoProvider: Function = IoC.Container.getType(TestNoProvider);
342+
expect(clazzNoProvider).to.be.equal(TestNoProvider);
343+
});
344+
345+
346+
});
347+
319348
describe("The IoC Container.snapshot(source) and Container.restore(source)", ()=>{
320349

321350
@IoC.AutoWired

0 commit comments

Comments
 (0)