Permalink
Browse files

Added blockParentFallbackProvider flag so that child injectors can op…

…t out of ancestor fallback providers.
  • Loading branch information...
1 parent 60179c0 commit b00907707421adb6be01dba37755f0207797310c @Stray Stray committed with Jul 17, 2012
Showing with 39 additions and 5 deletions.
  1. +3 −2 fallbackProviders.md
  2. +18 −3 src/org/swiftsuspenders/Injector.as
  3. +18 −0 test/org/swiftsuspenders/InjectorTests.as
View
5 fallbackProviders.md
@@ -15,7 +15,7 @@
A fallbackProvider is where the injector turns for help when it doesn't have a mapping.
-- Set through the `injector.fallbackProvider` property.
+- Set through the `injector.fallbackProvider` property (default is `null`).
- Either a class or instance.
- Must implement `FallbackDependencyProvider`.
- If a class is provided, a new instance of this class will be created for each dependency, with the dependency type passed to the constructor.
@@ -27,7 +27,7 @@ A fallbackProvider is where the injector turns for help when it doesn't have a m
1. Local mappings
2. Parent (and ancestor) mappings
3. Local fallbackProvider
-4. Parent (and ancestor) fallbackProviders
+4. Parent (and ancestor) fallbackProviders (unless the `blockParentFallbackProvider` flag is set)
5. Explode with `InjectorMissingMappingError`
### `satisfies` and `getInstance` are consistent
@@ -43,6 +43,7 @@ If there is a fallbackProvider it will be checked.
- Can support injection of interfaces if the fallbackProvider supports this.
- Can support async providers if you return a promise.
- Can be a facade delegating to a number of custom fallbackProviders.
+- Setting `blockParentFallbackProvider` flag on the injector prevents the injector from consulting ancestors. This only applies to fallback providers; parent mappings can still be used. Useful for child injectors in extensions.
## `ClassProvider` fits the most common use case
View
21 src/org/swiftsuspenders/Injector.as
@@ -168,10 +168,10 @@ package org.swiftsuspenders
* injections are started.
*/
public class Injector extends EventDispatcher
- {
+ {
//---------------------- Private / Protected Properties ----------------------//
private static var INJECTION_POINTS_CACHE : Dictionary = new Dictionary(true);
-
+
private var _parentInjector : Injector;
private var _applicationDomain:ApplicationDomain;
private var _classDescriptor : TypeDescriptor;
@@ -572,6 +572,21 @@ package org.swiftsuspenders
{
return providerMappings[getQualifiedClassName(type) + '|' + name] != null;
}
+
+ private var _blockParentFallbackProvider:Boolean = false;
+
+ public function get blockParentFallbackProvider():Boolean
+ {
+ return _blockParentFallbackProvider;
+ }
+
+ public function set blockParentFallbackProvider(value:Boolean):void
+ {
+ if (value !== _blockParentFallbackProvider)
+ {
+ _blockParentFallbackProvider = value;
+ }
+ }
//---------------------- Internal Methods ----------------------//
SsInternal static function purgeInjectionPointsCache() : void
@@ -620,7 +635,7 @@ package org.swiftsuspenders
SsInternal function getDefaultProvider(mappingId : String, consultParents : Boolean = true) : DependencyProvider
{
- if(!(_fallbackProvider || (consultParents && _parentInjector)))
+ if((!_fallbackProvider) && (_blockParentFallbackProvider || (!(consultParents && _parentInjector))))
{
return null;
}
View
18 test/org/swiftsuspenders/InjectorTests.as
@@ -1122,6 +1122,24 @@ package org.swiftsuspenders
assertThat(instance1, not(equalTo(instance2)));
}
+ [Test]
+ public function satisfies_doesnt_use_fallbackProvider_from_ancestors_if_blockParentFallbackProvider_is_set() : void
+ {
+ injector.fallbackProvider = ClassProvider;
+ const childInjector:Injector = injector.createChildInjector();
+ childInjector.blockParentFallbackProvider = true;
+ assertThat(childInjector.satisfies(Clazz), isFalse());
+ }
+
+ [Test(expects="org.swiftsuspenders.errors.InjectorMissingMappingError")]
+ public function getInstance_doesnt_use_fallbackProvider_from_ancestors_if_blockParentFallbackProvider_is_set() : void
+ {
+ injector.fallbackProvider = ClassProvider;
+ const childInjector:Injector = injector.createChildInjector();
+ childInjector.blockParentFallbackProvider = true;
+ childInjector.getInstance(Clazz);
+ }
+
// QUERY : This doesn't look like it's doing XML stuff??
[Test]

0 comments on commit b009077

Please sign in to comment.