Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added blockParentFallbackProvider flag so that child injectors can op…

…t out of ancestor fallback providers.
  • Loading branch information...
commit b00907707421adb6be01dba37755f0207797310c 1 parent 60179c0
Stray Stray authored committed
5 fallbackProviders.md
View
@@ -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
21 src/org/swiftsuspenders/Injector.as
View
@@ -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;
}
18 test/org/swiftsuspenders/InjectorTests.as
View
@@ -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]
Please sign in to comment.
Something went wrong with that request. Please try again.