Skip to content
Browse files

Realised that defending against names in getOrCreateNewInstance was s…

…illy, so just removed the ability to pass a name to the method.
  • Loading branch information...
1 parent d41d483 commit 60179c03c89c3345b8ec9e374d7c3a9b7654feef @Stray Stray committed with
Showing with 13 additions and 17 deletions.
  1. +9 −4 fallbackProviders.md
  2. +4 −8 src/org/swiftsuspenders/Injector.as
  3. +0 −5 test/org/swiftsuspenders/InjectorTests.as
View
13 fallbackProviders.md
@@ -53,6 +53,7 @@ If there is a fallbackProvider it will be checked.
- Regardless of any mappings.
- Ignores any fallbackProvider.
+- Can only be used for classes, not interfaces (type must have a constructor).
- Will use mappings / fallbackProvider for the dependency tree.
- In the case where you have relevant mappings but you don't want to use mappings / fallbackProvider for the dependency tree, you have options such as:
* Make a normal factory.
@@ -60,14 +61,18 @@ If there is a fallbackProvider it will be checked.
* Roll a fallbackProvider that forwards to a provider like the one in (2).
* Consider a different architecture, e.g. Entity based, for this part of the system.
-## `getOrCreateNewInstance` = `getInstance` or `instantiateUnmapped`
+## `getOrCreateNewInstance` = `getInstance` (unnamed) or `instantiateUnmapped`
-Provides a single point of entry to obtain a mapped instance if one exists, or a new instance if one doesn't.
+Provides a single point of entry to obtain a mapped instance if one exists, or a new instance if one doesn't, with identical constraints and flexibility to the methods it wraps.
-Essentially a sugar method for:
+Essentially a sugar method so:
+
+ var instance:SomeType = injector.getOrCreateNewInstance(SomeType);
+
+is equivalent to
var instance:SomeType = injector.satisfies(SomeType)
? injector.getInstance(SomeType)
: injector.instantiateUnmapped(SomeType);
-
+This method doesn't support names. If you know you need a name then you should always use `getInstance(type, name)`;
View
12 src/org/swiftsuspenders/Injector.as
@@ -553,17 +553,13 @@ package org.swiftsuspenders
return instance;
}
- public function getOrCreateNewInstance(type : Class, name : String = "") : *
+ public function getOrCreateNewInstance(type : Class) : *
{
- if(satisfies(type, name))
+ if(satisfies(type))
{
- return getInstance(type, name);
- }
- if(name.length > 0)
- {
- throw new InjectorError("Named injection always requires a valid mapping, and none was found for "
- + getQualifiedClassName(type) + '|' + name);
+ return getInstance(type);
}
+
return instantiateUnmapped(type);
}
View
5 test/org/swiftsuspenders/InjectorTests.as
@@ -1122,11 +1122,6 @@ package org.swiftsuspenders
assertThat(instance1, not(equalTo(instance2)));
}
- [Test(expects="org.swiftsuspenders.InjectorError")]
- public function getOrCreateNewInstance_throws_if_name_passed_and_cant_getInstance() : void
- {
- injector.getOrCreateNewInstance(Clazz, 'name');
- }
// QUERY : This doesn't look like it's doing XML stuff??
[Test]

0 comments on commit 60179c0

Please sign in to comment.
Something went wrong with that request. Please try again.