tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/590-using-multiple-interfaces-for-servicesRobotlegs: Discussion 2018-10-18T16:35:28Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/80275142011-06-19T19:30:53Z2011-06-19T19:30:53ZUsing multiple Interfaces for services<div><p>Hi Patrick,</p>
<p>just checking - do you mean you want to switch the JSON / AMF /
whatever service at runtime?</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/80275142011-06-19T19:40:33Z2011-06-19T19:40:33ZUsing multiple Interfaces for services<div><p>Stray,</p>
<p>Yes that's correct at runtime, depending which option is
selected - i<br>
have advanced settings.</p></div>patricklemiuextag:robotlegs.tenderapp.com,2009-10-18:Comment/80275142011-06-23T05:19:35Z2011-06-23T05:19:35ZUsing multiple Interfaces for services<div><p>Because re-injection into objects isn't currently supported in
Robotlegs/SwiftSuspenders 1.X, it's less than straightforward to
attempt to solve this by injection-mapping-switching on the fly,
especially if any persistent (read non-Command) objects have
lingering references to your service interface (I'm looking
meaningfully in the direction of the mediators).</p>
<p>So, one solution is to use a strategy pattern. Make an
implementation of your service interface that delegates all of the
real work to an <code>implementation</code> property. Then, use
commands to swap your different AMF / JSON variant objects into
that property on demand.</p>
<pre>
<code>interface IServiceExample {
function doSomething():void;
}
class WrapperServiceExample implements IServiceExample {
public var implementation:IServiceExample;
public function doSomething():void {
implementation.doSomething();
}
}
// in the context
injector.mapSingleton(AMFServiceExample);
injector.mapSingleton(JSONServiceExample);
injector.mapSingleton(WrapperServiceExample);
var wrapper:WrapperServiceExample = injector.getInstance(WrapperServiceExample);
injector.mapValue(IServiceExample, wrapper);
class SwapToUsingAMFService extends Command {
[Inject]
public var wrapper:WrapperServiceExample;
[Inject]
public var amfService:AMFServiceExample;
override public function execute():void {
wrapper.implementation = amfService;
}
}</code>
</pre></div>ZackPierce