huject
Advanced tools
Comparing version 1.2.0 to 1.2.1
declare module Huject { | ||
interface ContainerStatic { | ||
new(): ContainerImpl; | ||
} | ||
interface ContainerImpl { | ||
class Container { | ||
/** | ||
* @constructor | ||
*/ | ||
constructor(); | ||
/** | ||
* Allow to unregistered definitions be registered and resolved automatically. If false then each class should be explicitly registered in container | ||
* @param allow true to allow, false to disable | ||
*/ | ||
setAllowUnregisteredResolving(allow: boolean): void; | ||
/** | ||
* Register class definition | ||
@@ -12,3 +20,3 @@ * @param classDefinition Class definition | ||
*/ | ||
register<T>(classDefinition: Instantiable<T>, constructorArguments?: Array<any>): Definition; | ||
register(classDefinition: Function, constructorArguments?: Array<any>): Definition; | ||
/** | ||
@@ -20,3 +28,3 @@ * Bind class to another class (interface) | ||
*/ | ||
register<T>(interfaceDefinition: Instantiable<T>, implementationDefinition: Instantiable<T>, constructorArguments?: Array<any>): Definition; | ||
register(interfaceDefinition: Function, implementationDefinition: Function, constructorArguments?: Array<any>): Definition; | ||
/** | ||
@@ -27,3 +35,3 @@ * Bind pre-created object to class definition. The object will be used when defined class is instantiated | ||
*/ | ||
register<T>(classDefinition: Instantiable<T>, object: Object): Definition; | ||
register(classDefinition: Function, object: Object): Definition; | ||
@@ -36,3 +44,3 @@ /** | ||
*/ | ||
register<T>(symbolDefinition: string, classDefinition: Instantiable<T>, constructorArguments?: Array<any>): Definition; | ||
register(symbolDefinition: string, classDefinition: Function, constructorArguments?: Array<any>): Definition; | ||
/** | ||
@@ -50,3 +58,3 @@ * Bind object to string definition | ||
*/ | ||
registerCallable<T>(classDefinition: Instantiable<T>, callable: () => T): Definition; | ||
registerCallable(classDefinition: Function, callable: () => Object|Function): Definition; | ||
/** | ||
@@ -57,3 +65,3 @@ * Bind callable function to string definition. Instead creating new object the function result will be used instead | ||
*/ | ||
registerCallable<T>(symbolDefinition: string, callable: () => T): Definition; | ||
registerCallable(symbolDefinition: string, callable: () => Object|Function): Definition; | ||
@@ -65,3 +73,3 @@ /** | ||
*/ | ||
resolve<T>(definition: Instantiable<T>, method?: FactoryMethod): T; | ||
resolve(definition: Function, method?: FactoryMethod): any; | ||
/** | ||
@@ -75,6 +83,2 @@ * Resolve {instantiate} object from container by string definition. Will resolve all wired dependencies if they were specified by decorators | ||
interface Instantiable<T> { | ||
new(...args: Array<any>): T; | ||
} | ||
interface Definition { | ||
@@ -100,3 +104,2 @@ /** | ||
export var Container: ContainerStatic; | ||
@@ -103,0 +106,0 @@ /** |
{ | ||
"name": "huject", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Typescript dependency injection container for humans", | ||
@@ -36,3 +36,3 @@ "main": "./src/index.js", | ||
"source-map-support": "^0.3.2", | ||
"typescript": "^1.5.3" | ||
"typescript": "^1.6.0-beta" | ||
}, | ||
@@ -39,0 +39,0 @@ "dependencies": { |
@@ -46,3 +46,3 @@ # Typescript dependency injection for humans! | ||
*/ | ||
register<T>(classDefinition: Instantiable<T>, constructorArguments?: Array<any>): Definition; | ||
register(classDefinition: Function, constructorArguments?: Array<any>): Definition; | ||
/** | ||
@@ -54,3 +54,3 @@ * Bind class to another class (interface) | ||
*/ | ||
register<T>(interfaceDefinition: Instantiable<T>, implementationDefinition: Instantiable<T>, constructorArguments?: Array<any>): Definition; | ||
register(interfaceDefinition: Function, implementationDefinition: Function, constructorArguments?: Array<any>): Definition; | ||
/** | ||
@@ -61,3 +61,3 @@ * Bind pre-created object to class definition. The object will be used when defined class is instantiated | ||
*/ | ||
register<T>(classDefinition: Instantiable<T>, object: Object): Definition; | ||
register(classDefinition: Function, object: Object): Definition; | ||
/** | ||
@@ -69,3 +69,3 @@ * Bind class definition to string definition. Object could be later instantiated by resolve('symbol'); | ||
*/ | ||
register<T>(symbolDefinition: string, classDefinition: Instantiable<T>, constructorArguments?: Array<any>): Definition; | ||
register(symbolDefinition: string, classDefinition: Function, constructorArguments?: Array<any>): Definition; | ||
/** | ||
@@ -102,5 +102,2 @@ * Bind object to string definition | ||
// Container will catch error if you're trying to register incorrect implementation with interface | ||
container.register(MyInterface, WrongImplementation); // error | ||
// Register interface to implementation with constructor arguments. Arguments will overwrite previous arguments registration for MyInterfaceImplementation | ||
@@ -135,3 +132,3 @@ container.register(MyInterface, MyInterfaceImplementation, ['accesskey', 'accesstoken']); | ||
*/ | ||
registerCallable<T>(classDefinition: Instantiable<T>, callable: () => T): Definition; | ||
registerCallable(classDefinition: Function, callable: () => Object|Function): Definition; | ||
/** | ||
@@ -142,3 +139,3 @@ * Bind callable function to string definition. Instead creating new object the function result will be used instead | ||
*/ | ||
registerCallable<T>(symbolDefinition: string, callable: () => T): Definition; | ||
registerCallable(symbolDefinition: string, callable: () => Object|Function): Definition; | ||
``` | ||
@@ -166,3 +163,3 @@ | ||
*/ | ||
resolve<T>(definition: Instantiable<T>, method?: FactoryMethod): T; | ||
resolve(definition: Function, method?: FactoryMethod): any; | ||
/** | ||
@@ -517,3 +514,3 @@ * Resolve {instantiate} object from container by string definition. Will resolve all wired dependencies if they were specified by decorators | ||
### Use new typescript 1.6 abstract classes | ||
### Use new typescript 1.6 abstract classes (That is the best method i think) | ||
Since you can't instantiate abstract classes you can safely enable *container.setAllowUnregisteredResolving(true)*: | ||
@@ -547,3 +544,55 @@ | ||
``` | ||
**Note**: Any abstract methods will be omitted when compiling to JS. I'd suggest you to use empty function body {} and avoid use abstract method(), in *abstracted interfaces* but the choice is up to you. That doesn't impact any container functionality but impacts testing: | ||
```typescript | ||
abstract class ServiceInterface { | ||
public abstract method1(): void; | ||
} | ||
@ConstructorInject | ||
class Controller { | ||
private service: ServiceInterface; | ||
public constructor(service: ServiceInterface) { | ||
this.service = service; | ||
} | ||
public test(): void { | ||
this.service.method1(); | ||
} | ||
} | ||
// in test.ts | ||
let myMock = sinon.createStubInstance(ServiceInterface); | ||
let controller = new Controller(myMock); | ||
controller.test(); // Error | ||
``` | ||
The compiler will omit method1() from compiled JS file if method was declared as abstract. That affects the testing. | ||
```typescript | ||
abstract class ServiceInterface { | ||
public method1(): void {}; // empty function body instead of abstract | ||
} | ||
@ConstructorInject | ||
class Controller { | ||
private service: ServiceInterface; | ||
public constructor(service: ServiceInterface) { | ||
this.service = service; | ||
} | ||
public test(): void { | ||
this.service.method1(); | ||
} | ||
} | ||
// in test.ts | ||
let myMock = sinon.createStubInstance(ServiceInterface); | ||
let controller = new Controller(myMock); | ||
controller.test(); // OK since there was method1() emitted by compiler for service interface prototype | ||
myMock.method1.should.have.been.called; // OK | ||
``` | ||
This may looks weird though, so it's up to you which method to use. As i said it didn't affect any container functionality but might be useful for creating testing mocks/stubs. | ||
## Example | ||
@@ -550,0 +599,0 @@ In example/ directory you can see completely working DI example. To build you need to run grunt first |
@@ -10,17 +10,5 @@ { | ||
}, | ||
"files": [ | ||
"src/container.ts", | ||
"src/decorators.ts", | ||
"src/definition.ts", | ||
"src/index.ts", | ||
"tests/bootstrap.ts", | ||
"tests/container.spec.ts", | ||
"tests/decorators.spec.ts", | ||
"example/bootstrap.ts", | ||
"example/DeepService.ts", | ||
"example/FirstService.ts", | ||
"example/main.ts", | ||
"example/SecondService.ts", | ||
"example/ServiceInterface.ts" | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
289317
601
5359