diff --git a/src/hex/di/Injector.hx b/src/hex/di/Injector.hx index 6639a79..0b767ed 100644 --- a/src/hex/di/Injector.hx +++ b/src/hex/di/Injector.hx @@ -84,18 +84,18 @@ class Injector this.map( clazz, name ).toSingleton( type ); } - public function getInstance( type : Class, name : String = '' ) : T + public function getInstance( type : Class, name : String = '', targetType : Class = null ) : T { var mappingID = this._getMappingID( type, name ); var mapping = this._mapping[ mappingID ]; if ( mapping != null ) { - return mapping.getResult( null ); + return mapping.getResult( targetType ); } else if ( this._parentInjector != null ) { - return this._parentInjector.getInstance( type, name ); + return this._parentInjector.getInstance( type, name, targetType ); } else { @@ -104,18 +104,18 @@ class Injector } } - public function getInstanceWithClassName( className : String, name : String = '' ) : T + public function getInstanceWithClassName( className : String, name : String = '', targetType : Class = null ) : T { var mappingID = className + '|' + name; var mapping = cast this._mapping[ mappingID ]; if ( mapping != null ) { - return mapping.getResult( Type.getClass( className.split( '<' )[ 0 ] ) ); + return mapping.getResult( targetType ); } else if ( this._parentInjector != null ) { - return this._parentInjector.getInstanceWithClassName( className, name ); + return this._parentInjector.getInstanceWithClassName( className, name, targetType ); } else { diff --git a/test/hex/di/InjectorTest.hx b/test/hex/di/InjectorTest.hx index 5e015ca..793e915 100644 --- a/test/hex/di/InjectorTest.hx +++ b/test/hex/di/InjectorTest.hx @@ -888,4 +888,48 @@ class InjectorTest implements IInjectorListener Assert.equals(NamedClassInjecteeWithClassName, provider.target, "Target should be proper class"); Assert.equals("hex.di.mock.injectees.NamedClassInjecteeWithClassName", provider.className, "Target should be proper class"); } + + @Test( "Test dependency provider with class name" ) + public function testDependencyProviderWithClassName() : Void + { + var instance = new Clazz(); + var provider = new MockDependencyProvider(instance); + injector.map( Clazz ).toProvider(provider); + + var returnVal = injector.getInstanceWithClassName("hex.di.mock.types.Clazz"); + + Assert.equals(instance, returnVal, "Returned value must come from dependency provider"); + Assert.equals(injector, provider.injector, "Injector provided to the dependency provider must be the correct one"); + Assert.isNull(provider.target, "Target is unknown so it's supposed to be null"); + } + + @Test( "Test getInstance target type is passed to dependency provider" ) + public function testGetInstanceTargetTypeIsPassedToDependencyProvider() : Void + { + var instance = new Clazz(); + var provider = new MockDependencyProvider(instance); + injector.map( Clazz ).toProvider(provider); + + var returnVal = injector.getInstance(Clazz, null, NamedClassInjecteeWithClassName); + + Assert.equals(instance, returnVal, "Returned value must come from dependency provider"); + Assert.equals(injector, provider.injector, "Injector provided to the dependency provider must be the correct one"); + Assert.equals("hex.di.mock.injectees.NamedClassInjecteeWithClassName", provider.className, "Target should be proper class"); + } + + @Test( "Test getInstanceWithClassName target type is passed to dependency provider" ) + public function testGetInstanceWithClassNameTargetTypeIsPassedToDependencyProvider() : Void + { + var instance = new Clazz(); + var provider = new MockDependencyProvider(instance); + injector.map( Clazz ).toProvider(provider); + + var returnVal = injector.getInstanceWithClassName("hex.di.mock.types.Clazz", null, NamedClassInjecteeWithClassName); + + Assert.equals(instance, returnVal, "Returned value must come from dependency provider"); + Assert.equals(injector, provider.injector, "Injector provided to the dependency provider must be the correct one"); + Assert.equals("hex.di.mock.injectees.NamedClassInjecteeWithClassName", provider.className, "Target should be proper class"); + } + + } \ No newline at end of file diff --git a/test/hex/di/mock/provider/MockDependencyProvider.hx b/test/hex/di/mock/provider/MockDependencyProvider.hx index cfa55d9..d93f2eb 100644 --- a/test/hex/di/mock/provider/MockDependencyProvider.hx +++ b/test/hex/di/mock/provider/MockDependencyProvider.hx @@ -22,9 +22,6 @@ class MockDependencyProvider implements IDependencyProvider } - - /* INTERFACE hex.di.provider.IDependencyProvider.IDependencyProvider */ - public function getResult(injector:IDependencyInjector, target:Class):T { this.injector = injector; diff --git a/test/hex/di/util/InjectionUtilTest.hx b/test/hex/di/util/InjectionUtilTest.hx index a2b334e..d6b7b77 100644 --- a/test/hex/di/util/InjectionUtilTest.hx +++ b/test/hex/di/util/InjectionUtilTest.hx @@ -216,12 +216,12 @@ private class MockDependencyInjector implements IDependencyInjector } - public function getInstance( type : Class, name : String = '' ) : T + public function getInstance( type : Class, name : String = '', targetType : Class = null ) : T { return null; } - public function getInstanceWithClassName( className : String, name : String = '' ) : T + public function getInstanceWithClassName( className : String, name : String = '', targetType : Class = null ) : T { this.instanceWithClassName = { className: className, name: name }; return null;