Skip to content

Commit 7d45a67

Browse files
Merge pull request #67 from DoclerLabs/MappingDefinitionUtil_patch
injectInto happens only after everything is mapped
2 parents de967be + ba4f196 commit 7d45a67

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/hex/di/util/MappingDefinitionUtil.hx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class MappingDefinitionUtil
1818
mappings = hex.di.mapping.MappingChecker.filter( cls, mappings );
1919
}
2020

21+
var injectIntoValues = [];
22+
2123
for ( mapping in mappings )
2224
{
2325
if ( mapping.toValue != null )
@@ -38,8 +40,10 @@ class MappingDefinitionUtil
3840

3941
if ( mapping.injectInto )
4042
{
41-
target.injectInto( mapping.toValue );
43+
injectIntoValues.push(mapping.toValue);
4244
}
4345
}
46+
47+
injectIntoValues.map(target.injectInto);
4448
}
4549
}

test/hex/di/mapping/MappingDefinitionTest.hx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package hex.di.mapping;
22

3+
import hex.di.IInjectorContainer;
4+
import hex.di.Injector;
35
import hex.di.error.MissingMappingException;
46
import hex.di.mock.owner.DependencyOwner;
57
import hex.di.mock.types.Clazz;
@@ -59,6 +61,21 @@ class MappingDefinitionTest
5961
Assert.methodCallThrows( MissingMappingException, injector, injector.getInstanceWithClassName, [ "Void->String" ],
6062
"This mapping should have been filtered by the dependency checker" );
6163
}
64+
65+
@Test("test order of operations")
66+
public function testInjectIntoHappensAtLast()
67+
{
68+
var value = new Injectee();
69+
var mappings : Array<MappingDefinition> = [
70+
{ fromType: "hex.di.mapping.MappingDefinitionTest.Injectee", toValue: value, injectInto: true },
71+
{ fromType: "hex.di.mock.types.Interface", toClass: Clazz2, asSingleton: true }
72+
];
73+
74+
var injector = new Injector();
75+
mappings.addToInjector(injector);
76+
Assert.isNotNull(value.something);
77+
Assert.equals(injector.getInstance(Interface), value.something);
78+
}
6279
}
6380

6481
@Dependency( var _:Interface, "id" )
@@ -78,4 +95,13 @@ class DependencyOwner implements hex.di.mapping.IDependencyOwner
7895
}
7996

8097
public function getInjector() : IDependencyInjector return this._injector;
98+
}
99+
100+
class Injectee implements IInjectorContainer
101+
{
102+
@Inject
103+
@Optional(true)
104+
public var something:Interface;
105+
106+
public function new() { }
81107
}

0 commit comments

Comments
 (0)