Navigation Menu

Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
Changed the way InjectionConfig's are configured: They now receive th…
Browse files Browse the repository at this point in the history
…eir injector reference at creation time instead of through the InjectionPoint
  • Loading branch information
tschneidereit committed Dec 8, 2009
1 parent fb13cee commit 7cb6201
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/org/swiftsuspenders/InjectionConfig.as
@@ -1,2 +1,2 @@
/** Copyright (c) 2009 the original author or authors** Permission is hereby granted, free of charge, to any person obtaining a copy* of this software and associated documentation files (the "Software"), to deal* in the Software without restriction, including without limitation the rights* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell* copies of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in* all copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN* THE SOFTWARE.*/package org.swiftsuspenders{ import flash.utils.Dictionary; import org.swiftsuspenders.injectionresults.IInjectionResult; import org.swiftsuspenders.injectionresults.InjectClassResult; import org.swiftsuspenders.injectionresults.InjectNullResult; import org.swiftsuspenders.injectionresults.InjectSingletonResult; import org.swiftsuspenders.injectionresults.InjectValueResult;
public class InjectionConfig { /******************************************************************************************* * public properties * *******************************************************************************************/ public var request : Class; public var response : Object; public var injectionType : int; public var injectionName : String; /******************************************************************************************* * public methods * *******************************************************************************************/ public function InjectionConfig(request : Class, response : Object, injectionType : int, injectionName : String) { this.request = request; this.response = response; this.injectionType = injectionType; this.injectionName = injectionName; } public function getResponse(injector : Injector, singletons : Dictionary) : Object { return createResultByInjectionType().getResponse(injector, singletons); } /******************************************************************************************* * private methods * *******************************************************************************************/ private function createResultByInjectionType() : IInjectionResult { switch (injectionType) { case InjectionType.CLASS: { return new InjectClassResult(this); } case InjectionType.VALUE: { return new InjectValueResult(this); } case InjectionType.SINGLETON: { return new InjectSingletonResult(this); } default: { break; } } return new InjectNullResult(); } }}
/** Copyright (c) 2009 the original author or authors* * Permission is hereby granted to use, modify, and distribute this file * in accordance with the terms of the license agreement accompanying it.*/package org.swiftsuspenders{ import flash.utils.Dictionary; import org.swiftsuspenders.injectionresults.IInjectionResult; import org.swiftsuspenders.injectionresults.InjectClassResult; import org.swiftsuspenders.injectionresults.InjectNullResult; import org.swiftsuspenders.injectionresults.InjectSingletonResult; import org.swiftsuspenders.injectionresults.InjectValueResult;
public class InjectionConfig { /******************************************************************************************* * public properties * *******************************************************************************************/ public var request : Class; public var response : Object; public var injectionType : int; public var injectionName : String; public var injector : Injector; /******************************************************************************************* * public methods * *******************************************************************************************/ public function InjectionConfig(request : Class, response : Object, injectionType : int, injectionName : String, injector : Injector) { this.request = request; this.response = response; this.injectionType = injectionType; this.injectionName = injectionName; this.injector = injector; } public function getResponse(singletons : Dictionary) : Object { return createResultByInjectionType().getResponse(injector, singletons); } /******************************************************************************************* * private methods * *******************************************************************************************/ private function createResultByInjectionType() : IInjectionResult { switch (injectionType) { case InjectionType.CLASS: { return new InjectClassResult(this); } case InjectionType.VALUE: { return new InjectValueResult(this); } case InjectionType.SINGLETON: { return new InjectSingletonResult(this); } default: { break; } } return new InjectNullResult(); } }}
Expand Down
15 changes: 10 additions & 5 deletions src/org/swiftsuspenders/Injector.as
Expand Up @@ -52,7 +52,7 @@ package org.swiftsuspenders
whenAskedFor : Class, useValue : Object, named : String = "") : *
{
var config : InjectionConfig = new InjectionConfig(
whenAskedFor, useValue, InjectionType.VALUE, named);
whenAskedFor, useValue, InjectionType.VALUE, named, this);
addMapping(config, named);
return config;
}
Expand All @@ -61,7 +61,7 @@ package org.swiftsuspenders
whenAskedFor : Class, instantiateClass : Class, named : String = "") : *
{
var config : InjectionConfig = new InjectionConfig(
whenAskedFor, instantiateClass, InjectionType.CLASS, named);
whenAskedFor, instantiateClass, InjectionType.CLASS, named, this);
addMapping(config, named);
return config;
}
Expand All @@ -75,7 +75,7 @@ package org.swiftsuspenders
whenAskedFor : Class, useSingletonOf : Class, named : String = "") : *
{
var config : InjectionConfig = new InjectionConfig(
whenAskedFor, useSingletonOf, InjectionType.SINGLETON, named);
whenAskedFor, useSingletonOf, InjectionType.SINGLETON, named, this);
addMapping(config, named);
return config;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ package org.swiftsuspenders
for (var i : int = 0; i < length; i++)
{
var injectionPoint : InjectionPoint = injectionPoints[i];
injectionPoint.applyInjection(target, this, m_singletons);
injectionPoint.applyInjection(target, m_singletons);
}

}
Expand All @@ -130,7 +130,7 @@ package org.swiftsuspenders
getInjectionPoints(clazz);
injectionPoint = m_constructorInjectionPoints[clazz];
}
var instance : * = injectionPoint.applyInjection(clazz, this, m_singletons);
var instance : * = injectionPoint.applyInjection(clazz, m_singletons);
injectInto(instance);
return instance;
}
Expand All @@ -148,6 +148,11 @@ package org.swiftsuspenders
}
}

public function createChildInjector() : Injector
{
return new Injector();
}


/*******************************************************************************************
* protected/ private methods *
Expand Down
Expand Up @@ -33,10 +33,9 @@ package org.swiftsuspenders.injectionpoints
super(node, injectorMappings);
}

override public function applyInjection(
target : Object, injector : Injector, singletons : Dictionary) : Object
override public function applyInjection(target : Object, singletons : Dictionary) : Object
{
var p : Array = gatherParameterValues(target, injector, singletons);
var p : Array = gatherParameterValues(target, singletons);
//the only way to implement ctor injections, really!
switch (p.length)
{
Expand Down
8 changes: 7 additions & 1 deletion src/org/swiftsuspenders/injectionpoints/InjectionPoint.as
Expand Up @@ -12,6 +12,12 @@ package org.swiftsuspenders.injectionpoints

public class InjectionPoint
{
/*******************************************************************************************
* protected properties *
*******************************************************************************************/
protected var injectionName : String;


/*******************************************************************************************
* public methods *
*******************************************************************************************/
Expand All @@ -20,7 +26,7 @@ package org.swiftsuspenders.injectionpoints
initializeInjection(node, injectorMappings);
}

public function applyInjection(target : Object, injector : Injector, singletons : Dictionary) : Object
public function applyInjection(target : Object, singletons : Dictionary) : Object
{
return target;
}
Expand Down
10 changes: 4 additions & 6 deletions src/org/swiftsuspenders/injectionpoints/MethodInjectionPoint.as
Expand Up @@ -32,10 +32,9 @@ package org.swiftsuspenders.injectionpoints
super(node, injectorMappings);
}

override public function applyInjection(
target : Object, injector : Injector, singletons : Dictionary) : Object
override public function applyInjection(target : Object, singletons : Dictionary) : Object
{
var parameters : Array = gatherParameterValues(target, injector, singletons);
var parameters : Array = gatherParameterValues(target, singletons);
var method : Function = target[methodName];
method.apply(target, parameters);
return target;
Expand Down Expand Up @@ -91,8 +90,7 @@ package org.swiftsuspenders.injectionpoints
}
}

protected function gatherParameterValues(
target : Object, injector : Injector, singletons : Dictionary) : Array
protected function gatherParameterValues(target : Object, singletons : Dictionary) : Array
{
var parameters : Array = [];
var length : int = mappings.length;
Expand All @@ -112,7 +110,7 @@ package org.swiftsuspenders.injectionpoints
));
}

var injection : Object = config.getResponse(injector, singletons);
var injection : Object = config.getResponse(singletons);
parameters[i] = injection;
}
return parameters;
Expand Down
Expand Up @@ -17,7 +17,7 @@ package org.swiftsuspenders.injectionpoints
super(null, null);
}

override public function applyInjection(target : Object, injector : Injector, singletons : Dictionary) : Object
override public function applyInjection(target : Object, singletons : Dictionary) : Object
{
return new target();
}
Expand Down
Expand Up @@ -33,8 +33,7 @@ package org.swiftsuspenders.injectionpoints
return orderValue;
}

override public function applyInjection(
target : Object, injector : Injector, singletons : Dictionary) : Object
override public function applyInjection(target : Object, singletons : Dictionary) : Object
{
target[methodName]();
return target;
Expand Down
Expand Up @@ -31,8 +31,7 @@ package org.swiftsuspenders.injectionpoints
super(node, injectorMappings);
}

override public function applyInjection(
target : Object, injector : Injector, singletons : Dictionary) : Object
override public function applyInjection(target : Object, singletons : Dictionary) : Object
{
var config : InjectionConfig = mappings[propertyType];
if (!config)
Expand All @@ -44,7 +43,7 @@ package org.swiftsuspenders.injectionpoints
)
);
}
var injection : Object = config.getResponse(injector, singletons);
var injection : Object = config.getResponse(singletons);
target[propertyName] = injection;
return target;
}
Expand All @@ -58,11 +57,11 @@ package org.swiftsuspenders.injectionpoints
var mappings : Dictionary;
if (node.hasOwnProperty('arg') && node.arg.(@key == 'name').length)
{
var name : String = node.arg.@value.toString();
mappings = injectorMappings[name];
injectionName = node.arg.@value.toString();
mappings = injectorMappings[injectionName];
if (!mappings)
{
mappings = injectorMappings[name] = new Dictionary();
mappings = injectorMappings[injectionName] = new Dictionary();
}
}
else
Expand Down
34 changes: 17 additions & 17 deletions test/org/swiftsuspenders/InjectionConfigTests.as
Expand Up @@ -25,7 +25,7 @@ package org.swiftsuspenders
public function configIsInstantiated():void
{
var config : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "");
Clazz, Clazz, InjectionType.SINGLETON, "", null);

Assert.assertTrue(config is InjectionConfig);
}
Expand All @@ -35,8 +35,8 @@ package org.swiftsuspenders
{
var response:Clazz = new Clazz();
var config : InjectionConfig = new InjectionConfig(
Clazz, response, InjectionType.VALUE, "");
var returnedResponse:Object = config.getResponse( injector, null);
Clazz, response, InjectionType.VALUE, "", injector);
var returnedResponse:Object = config.getResponse(null);

Assert.assertStrictlyEquals(response, returnedResponse);
}
Expand All @@ -45,8 +45,8 @@ package org.swiftsuspenders
public function injectionTypeClassReturnsRespone():void
{
var config : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.CLASS, "");
var returnedResponse:Object = config.getResponse( injector, null);
Clazz, Clazz, InjectionType.CLASS, "", injector);
var returnedResponse:Object = config.getResponse(null);

Assert.assertTrue( returnedResponse is Clazz);
}
Expand All @@ -56,8 +56,8 @@ package org.swiftsuspenders
{
var singletons:Dictionary = new Dictionary();
var config : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "");
var returnedResponse:Object = config.getResponse( injector, singletons);
Clazz, Clazz, InjectionType.SINGLETON, "", injector);
var returnedResponse:Object = config.getResponse(singletons);

Assert.assertTrue( returnedResponse is Clazz);
}
Expand All @@ -67,8 +67,8 @@ package org.swiftsuspenders
{
var singletons:Dictionary = new Dictionary();
var config : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "");
var returnedResponse:Object = config.getResponse( injector, singletons);
Clazz, Clazz, InjectionType.SINGLETON, "", injector);
var returnedResponse:Object = config.getResponse(singletons);

Assert.assertTrue( singletons[Clazz] == returnedResponse );
}
Expand All @@ -78,8 +78,8 @@ package org.swiftsuspenders
{
var singletons:Dictionary = new Dictionary();
var config : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "named");
var returnedResponse:Object = config.getResponse( injector, singletons);
Clazz, Clazz, InjectionType.SINGLETON, "named", injector);
var returnedResponse:Object = config.getResponse(singletons);

Assert.assertTrue( singletons["named"][Clazz] == returnedResponse );
}
Expand All @@ -89,9 +89,9 @@ package org.swiftsuspenders
{
var singletons:Dictionary = new Dictionary();
var config : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "");
var returnedResponse:Object = config.getResponse( injector, singletons);
var secondResponse:Object = config.getResponse( injector, singletons);
Clazz, Clazz, InjectionType.SINGLETON, "", injector);
var returnedResponse:Object = config.getResponse(singletons);
var secondResponse:Object = config.getResponse(singletons);

Assert.assertStrictlyEquals( returnedResponse, secondResponse )
}
Expand All @@ -101,9 +101,9 @@ package org.swiftsuspenders
{
var singletons:Dictionary = new Dictionary();
var config : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "named");
var returnedResponse:Object = config.getResponse( injector, singletons);
var secondResponse:Object = config.getResponse( injector, singletons);
Clazz, Clazz, InjectionType.SINGLETON, "named", injector);
var returnedResponse:Object = config.getResponse(singletons);
var secondResponse:Object = config.getResponse(singletons);

Assert.assertStrictlyEquals( returnedResponse, secondResponse )
}
Expand Down
Expand Up @@ -29,10 +29,9 @@ package org.swiftsuspenders.injectionpoints
public function injectionOfFirstOptionalPropertyIntoTwoOptionalParametersConstructor():void
{
var injector:Injector = new Injector();

var mappings:Dictionary = new Dictionary();
var config_clazz : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "");
Clazz, Clazz, InjectionType.SINGLETON, "", injector);
var fqcn_clazz:String = getQualifiedClassName(Clazz);
mappings[fqcn_clazz] = config_clazz;

Expand All @@ -41,7 +40,7 @@ package org.swiftsuspenders.injectionpoints

var singletons:Dictionary = new Dictionary();
var injectee:TwoOptionalParametersConstructorInjectee =
injectionPoint.applyInjection(TwoOptionalParametersConstructorInjectee, injector, singletons) as TwoOptionalParametersConstructorInjectee;
injectionPoint.applyInjection(TwoOptionalParametersConstructorInjectee, singletons) as TwoOptionalParametersConstructorInjectee;


Assert.assertTrue("dependency 1 should be Clazz instance", injectee.getDependency() is Clazz);
Expand All @@ -52,10 +51,9 @@ package org.swiftsuspenders.injectionpoints
public function injectionOfSecondOptionalPropertyIntoTwoOptionalParametersConstructor():void
{
var injector:Injector = new Injector();

var mappings:Dictionary = new Dictionary();
var string_reference : InjectionConfig = new InjectionConfig(
String, STRING_REFERENCE, InjectionType.VALUE, "");
String, STRING_REFERENCE, InjectionType.VALUE, "", injector);
var fqcn_string:String = getQualifiedClassName(String);
mappings[fqcn_string] = string_reference;

Expand All @@ -64,7 +62,7 @@ package org.swiftsuspenders.injectionpoints

var singletons:Dictionary = new Dictionary();
var injectee:TwoOptionalParametersConstructorInjectee =
injectionPoint.applyInjection(TwoOptionalParametersConstructorInjectee, injector, singletons) as TwoOptionalParametersConstructorInjectee;
injectionPoint.applyInjection(TwoOptionalParametersConstructorInjectee, singletons) as TwoOptionalParametersConstructorInjectee;


Assert.assertTrue("dependency 1 should be Clazz null", injectee.getDependency() == null);
Expand All @@ -73,11 +71,10 @@ package org.swiftsuspenders.injectionpoints

private function applyConstructorInjectionToTwoUnamedParameterInjectee():TwoParametersConstructorInjectee
{
var injector:Injector = new Injector();
var injectionPoint:ConstructorInjectionPoint = createTwoPropertySingletonClazzAndInterfaceConstructorInjectionPoint();
var singletons:Dictionary = new Dictionary();
var injectee:TwoParametersConstructorInjectee =
injectionPoint.applyInjection(TwoParametersConstructorInjectee, injector, singletons) as TwoParametersConstructorInjectee;
injectionPoint.applyInjection(TwoParametersConstructorInjectee, singletons) as TwoParametersConstructorInjectee;

return injectee;
}
Expand All @@ -92,11 +89,12 @@ package org.swiftsuspenders.injectionpoints

private function createUnamedTwoPropertyPropertySingletonInjectionConfigDictionary():Dictionary
{
var injector:Injector = new Injector();
var configDictionary:Dictionary = new Dictionary();
var config_clazz : InjectionConfig = new InjectionConfig(
Clazz, Clazz, InjectionType.SINGLETON, "");
Clazz, Clazz, InjectionType.SINGLETON, "", injector);
var string_reference : InjectionConfig = new InjectionConfig(
String, STRING_REFERENCE, InjectionType.VALUE, "");
String, STRING_REFERENCE, InjectionType.VALUE, "", injector);
var fqcn_clazz:String = getQualifiedClassName(Clazz);
var fqcn_string:String = getQualifiedClassName(String);

Expand Down

0 comments on commit 7cb6201

Please sign in to comment.