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

Commit

Permalink
Re-integrated named injections into the default injector. All mapping…
Browse files Browse the repository at this point in the history
…s now have an optional "name" parameter again. This changed shaved off about 150 LOC and is preparation for vast changes to dependency resolution.
  • Loading branch information
tschneidereit committed Oct 28, 2011
1 parent ba75267 commit b181018
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 307 deletions.
16 changes: 7 additions & 9 deletions src/org/swiftsuspenders/InjectionRule.as
Expand Up @@ -20,6 +20,7 @@ package org.swiftsuspenders
{
//---------------------- Private / Protected Properties ----------------------//
protected var _requestClass : Class;
protected var _mappingId : String;
protected var _injector : Injector;

private var _creatingInjector : Injector;
Expand All @@ -32,10 +33,12 @@ package org.swiftsuspenders


//---------------------- Public Methods ----------------------//
public function InjectionRule(creatingInjector : Injector, requestClass : Class)
public function InjectionRule(
creatingInjector : Injector, type : Class, mappingId : String)
{
_creatingInjector = creatingInjector;
_requestClass = requestClass;
_requestClass = type;
_mappingId = mappingId;
}

/**
Expand Down Expand Up @@ -102,7 +105,7 @@ package org.swiftsuspenders
if (_provider != null && provider != null)
{
//TODO: consider making this throw
trace('Warning: Injector already has a rule for ' + describeInjection() + '.\n ' +
trace('Warning: Injector already has a rule for ' + _mappingId + '.\n ' +
'If you have overwritten this mapping intentionally you can use ' +
'"injector.unmap()" prior to your replacement mapping in order to ' +
'avoid seeing this message.');
Expand Down Expand Up @@ -130,12 +133,7 @@ package org.swiftsuspenders
//---------------------- Private / Protected Methods ----------------------//
protected function getParentRule(injector : Injector) : InjectionRule
{
return (_injector || injector).SsInternal::getAncestorMapping(_requestClass);
}

protected function describeInjection() : String
{
return 'type "' + getQualifiedClassName(_requestClass) + '"';
return (_injector || injector).SsInternal::getAncestorMapping(_mappingId);
}
}
}
96 changes: 33 additions & 63 deletions src/org/swiftsuspenders/Injector.as
Expand Up @@ -29,30 +29,30 @@ package org.swiftsuspenders
private var _applicationDomain:ApplicationDomain;
private var _classDescriptor : ClassDescriptor;
private var _mappings : Dictionary;
private var _namedInjectionsManager : NamedInjectionsManager;


//---------------------- Public Methods ----------------------//
public function Injector()
{
_mappings = new Dictionary();
_namedInjectionsManager = new NamedInjectionsManager(this);
_classDescriptor = new ClassDescriptor(INJECTION_POINTS_CACHE);
_applicationDomain = ApplicationDomain.currentDomain;
}

public function map(dependency : Class) : InjectionRule
public function map(type : Class, name : String = '') : InjectionRule
{
return _mappings[dependency] ||= createRule(dependency);
const mappingId : String = getQualifiedClassName(type) + '|' + name;
return _mappings[mappingId] ||= createRule(type, mappingId);
}

public function unmap(dependency : Class) : void
public function unmap(type : Class, name : String = '') : void
{
var rule : InjectionRule = _mappings[dependency];
const mappingId : String = getQualifiedClassName(type) + '|' + name;
var rule : InjectionRule = _mappings[mappingId];
if (!rule)
{
throw new InjectorError('Error while removing an injector mapping: ' +
'No rule defined for dependency ' + getQualifiedClassName(dependency));
'No rule defined for dependency ' + mappingId);
}
rule.setProvider(null);
}
Expand All @@ -61,12 +61,12 @@ package org.swiftsuspenders
* Indicates whether the injector can supply a response for the specified dependency either
* by using a rule mapped directly on itself or by querying one of its ancestor injectors.
*
* @param dependency The dependency under query
* @param type The dependency under query
* @return <code>true</code> if the dependency can be satisfied, <code>false</code> if not
*/
public function satisfies(dependency : Class) : Boolean
public function satisfies(type : Class, name : String = '') : Boolean
{
var rule : InjectionRule = getMapping(dependency);
const rule : InjectionRule = getMapping(getQualifiedClassName(type) + '|' + name);
return rule && rule.hasProvider();
}

Expand All @@ -77,12 +77,12 @@ package org.swiftsuspenders
* In contrast to <code>satisfies</code>, <code>satisfiesDirectly</code> only informs
* about rules mapped directly on itself, without querying its ancestor injectors.
*
* @param dependency The dependency under query
* @param type The dependency under query
* @return <code>true</code> if the dependency can be satisfied, <code>false</code> if not
*/
public function satisfiesDirectly(dependency : Class) : Boolean
public function satisfiesDirectly(type : Class, name : String = '') : Boolean
{
var rule : InjectionRule = _mappings[dependency];
const rule : InjectionRule = _mappings[getQualifiedClassName(type) + '|' + name];
return rule && rule.hasProvider();
}

Expand All @@ -99,13 +99,14 @@ package org.swiftsuspenders
* @return The rule mapped to the specified dependency class
* @throws InjectorError when no rule was found for the specified dependency
*/
public function getRule(type : Class) : InjectionRule
public function getRule(type : Class, name : String = '') : InjectionRule
{
var rule : InjectionRule = _mappings[type];
const mappingId : String = getQualifiedClassName(type) + '|' + name;
var rule : InjectionRule = _mappings[mappingId];
if (!rule)
{
throw new InjectorError('Error while retrieving an injector mapping: ' +
'No rule defined for dependency ' + getQualifiedClassName(type));
'No rule defined for dependency ' + mappingId);
}
return rule;
}
Expand All @@ -118,13 +119,19 @@ package org.swiftsuspenders
applyInjectionPoints(target, type, ctorInjectionPoint.next);
}

public function getInstance(type : Class) : *
public function getInstance(type : Class, name : String = '') : *
{
var mapping : InjectionRule = getMapping(type);
const mappingId : String = getQualifiedClassName(type) + '|' + name;
var mapping : InjectionRule = getMapping(mappingId);
if (mapping && mapping.hasProvider())
{
return mapping.apply(type, this);
}
if (name)
{
throw new InjectorError('No mapping found for request ' + mappingId
+ '. getInstance only creates an unmapped instance if no name is given.');
}
return instantiateUnmapped(type);
}

Expand Down Expand Up @@ -154,37 +161,21 @@ package org.swiftsuspenders
return _applicationDomain;
}

public function usingName(name : String) : NamedInjectionsManager
{
_namedInjectionsManager.setRequestName(name);
return _namedInjectionsManager;
}


//---------------------- Internal Methods ----------------------//
SsInternal static function purgeInjectionPointsCache() : void
{
INJECTION_POINTS_CACHE = new Dictionary(true);
}

SsInternal function getAncestorMapping(requestType : Class) : InjectionRule
SsInternal function getAncestorMapping(mappingId : String) : InjectionRule
{
if (_parentInjector)
{
return _parentInjector.getMapping(requestType);
}
return null;
return _parentInjector ? _parentInjector.getMapping(mappingId) : null;
}

SsInternal function getRuleForInjectionPointConfig(
config : InjectionPointConfig) : InjectionRule
SsInternal function getMapping(mappingId : String) : InjectionRule
{
if (config.injectionName)
{
_namedInjectionsManager.setRequestName(config.injectionName);
return _namedInjectionsManager.getMappingByName(config.typeName);
}
return getMappingByName(config.typeName);
return _mappings[mappingId] || getAncestorMapping(mappingId);
}

SsInternal function instantiateUnmapped(type : Class) : Object
Expand All @@ -203,34 +194,13 @@ package org.swiftsuspenders


//---------------------- Private / Protected Methods ----------------------//
private function createRule(requestType : Class) : InjectionRule
private function createRule(type : Class, mappingId : String) : InjectionRule
{
const rule : InjectionRule = new InjectionRule(this, requestType);
_mappings[getQualifiedClassName(requestType)] = rule;
return rule;
}

private function getMapping(requestType : Class) : InjectionRule
{
return _mappings[requestType] || getAncestorMapping(requestType);
}

private function getMappingByName(requestTypeName : String) : InjectionRule
{
return _mappings[requestTypeName] || getAncestorMappingByName(requestTypeName);
}

private function getAncestorMappingByName(requestTypeName : String) : InjectionRule
{
if (_parentInjector)
{
return _parentInjector.getMappingByName(requestTypeName);
}
return null;
return new InjectionRule(this, type, mappingId);
}

private function applyInjectionPoints(target : Object, targetType : Class,
injectionPoint : InjectionPoint) : void
private function applyInjectionPoints(
target : Object, targetType : Class, injectionPoint : InjectionPoint) : void
{
while (injectionPoint)
{
Expand Down
37 changes: 0 additions & 37 deletions src/org/swiftsuspenders/NamedInjectionRule.as

This file was deleted.

0 comments on commit b181018

Please sign in to comment.