tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2351-runtime-injectionRobotlegs: Discussion 2013-06-21T10:13:54Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/272598622013-06-11T16:16:48Z2013-06-11T16:16:48ZRuntime Injection <div><p>Hello Tejpal,</p>
<p>Hopefully, I understood your question correctly: you want to
inject a class for which you haven’t provided a mapping in
your context, right?</p>
<p>In this case, you have to map the class right
<strong>before</strong> the injection occurs. Without a mapping
(rules) the Injector cannot handle the injection, as you probably
know.</p>
<p>Let’s say you had 2 Models, SomeModel and AnotherModel,
both implementing an interface - IModel.<br>
Depending on some conditions, you’d want to switch between
the 2 models inside a command. In the command you’d inject
the interface:</p>
<p>[Inject] public var model:IModel;</p>
<p><strong>Before</strong> triggering the command, you’d have
to map the model that you want to be used, like this:</p>
<p>injector.mapSingletonOf(IModel, SomeModel);</p>
<p>or</p>
<p>injector.mapSingletonOf(IModel, AnotherModel);</p>
<p>Let us know whether this answers your question or not. If it
doesn’t, then please give us more details about what you mean
by “runtime injection”.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272598622013-06-12T05:08:38Z2013-06-12T05:08:38ZRuntime Injection <div><p>Thank you very much.. but my exact problem is , i wanted to pass
some argument to the injecting class so that the injection will be
done depending on the parameter which i passed...<br>
suppose i have a<br>
class Base{<br>
} class Child extends Base{</p>
<p>} class Try{ [Inject ( id="Passed Argument")]
public var b:Base;<br>
[Inject ( id="Passed Argument")] public var
c:Child; } class C{ [Inject (id="can I Pass some Argu.
Here")] var t:Try;</p>
<p>}</p>
<p>I want to pass parameters from Class C to Try Class. Then the
Try Class injects Base and Child Class Depending on these
parameters.. So Is it possible in Robotlegs?</p>
<p>Thanks and Regards</p>
<p>TEJPAL B. BORKAR</p>
<p><a href=
"mailto:tejpalborkar_10@rediffmail.com">tejpalborkar_10@rediffmail.com</a></p>
<p>mb. no. 9096865304</p></div>Tejpal Borkartag:robotlegs.tenderapp.com,2009-10-18:Comment/272598622013-06-12T14:50:38Z2013-06-12T15:20:11ZRuntime Injection <div><p>You’re welcome, Tejpal.</p>
<blockquote>
<p>[Inject(id = "can I Pass some Argu. Here")] var t:Try;</p>
</blockquote>
<p>No, you can’t pass arguments like this.<br>
You either pass the properties by accessing an API of the class
that needs them, or you inject them in every class, for example as
a VO.</p>
<blockquote>
<p>Then the Try Class injects Base and Child Class Depending on
these parameters.</p>
</blockquote>
<p>Personally, I don’t know of a way of making the injection
conditional in robotlegs version 1. Maybe I’m missing
something….</p>
<p>SwiftSuspenders version 2 introduced optional injection, but
I’m not sure whether the latest rl1 supports SwiftSuspenders
2.</p>
<p>Anyway, with optional injection, you could have something like
this:</p>
<p>[Inject (optional=true)] public var childOne:ChildOneClass;</p>
<p>[Inject (optional=true)] public var childTwo:ChildTwoClass;</p>
<p>If, for example, there is only a mapping for childOne,
injector.mapSingleton(ChildOneClass);, and none for childTwo, you
could check if childTwo is null before trying to use it.<br>
Say, in your class C you’d map the class that you wanted to
be used inside Try. Then in class Try you’d check which one
is available.<br>
Of course you’d have to unmap it manually, if need be.</p>
<h2><a href="#" name="" class="anchor"></a></h2>
<p>In robotlegs version 2 you could make use of the new features,
guards and hooks, to make decisions depending on some
conditions.</p>
<p>In my example bellow, there is a GrandparentClass (like your
class C) that has a ParentClass (class Try) injected into it.
ChildOneClass and ChildTwoClass are like your classes that you
wanted to be injected into the Try class.</p>
<p>GrandparentClass sets a property like this:<br>
someVO.someCondition = "one";</p>
<p>Then it calls parentClass.switchChildren();<br>
SomeVO has been injected into ParentClass, so ParentClass will have
access to someVO.someCondition.</p>
<p>guardsApprove([approve]) will check against a condition inside
of the aprove() method, which will return true or false.</p>
<p>So, if someVO.someCondition==”one”, the guards
return true, and ChildOneClass will be instantiated. Otherwise
ChildTwoClass will be instantiated. SomeVO has been injected into
ChildOneClass and ChildTwoClass as well, and they can set or get
its properties.</p>
<p>Guards and hooks can be used in various ways. This is just a
simple example.</p>
<p>SomeVO</p>
<pre>
<code>
public class SomeVO
{
private var _someCondition:String;
private var _someProperty:String;
public function SomeVO()
{
}
public function get someProperty():String
{
return _someProperty;
}
public function set someProperty(value:String):void
{
_someProperty = value;
}
public function get someCondition():String
{
return _someCondition;
}
public function set someCondition(value:String):void
{
_someCondition = value;
}
}</code>
</pre>
<p>Grandparent</p>
<pre>
<code> public class GrandparentClass
{
[Inject]
public var parentClass:ParentClass;
[Inject]
public var someVO:SomeVO;
public function GrandparentClass()
{
}
public function setSomeParams():void
{
someVO.someCondition = "one";
parentClass.switchChildren();
}
}</code>
</pre>
<p>ParentClass</p>
<pre>
<code>
public class ParentClass
{
[Inject]
public var someVO:SomeVO;
[Inject]
public var injector:IInjector;
public function ParentClass()
{
}
public function switchChildren():void
{
someVO.someProperty = "Your parent says hi";
if (guardsApprove([approve]))
{
applyHooks([ChildOneClass], injector);
}
else
{
applyHooks([ChildTwoClass], injector);
}
}
public function approve():Boolean
{
return someVO.someCondition == "one";
}
}</code>
</pre>
<p>ChildOneClass</p>
<pre>
<code> public class ChildOneClass
{
[Inject]
public var someVO:SomeVO;
public function ChildOneClass()
{
}
public function hook():void
{
//do whatever you need to do in here
trace("ChildOneClass.hook() " + someVO.someCondition);
}
}</code>
</pre>
<p>ChildTwoClass</p>
<pre>
<code> public class ChildTwoClass
{
[Inject]
public var someVO:SomeVO;
public function ChildTwoClass()
{
}
public function hook():void
{
//do whatever you need to do in here
trace("ChildTwoClass.hook() " + someVO.someProperty);
//will trace Your parent says hi,
//set in ParentClass.switchChildren
}
}</code>
</pre>
<p>Cheers,<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272598622013-06-13T12:51:53Z2013-06-13T12:51:53ZRuntime Injection <div><p>Thanks sir..Its absolutely correct sir... but i just want
to make very few changes in my existing application. so suggest
some solution by using naming injection..in my application its some
what like as follows..<br>
class Try{<br>
some function{ // here i want to pass parameters to
Someclass to inject its reference variables<br>
} } class SomeClass{ [Inject (name="passed par")] var
b:Base;<br>
// and many more Inject variables to inject according to the
parameters } in context class<br>
lets say <br>
class MyContext{function startup()<br>
{ injector.mapSingleton(SomeClass, "InjectBase");
injector.mapSingleton(SomeClass, "InjectChild");}
}</p>
<p>Thanks and Regards</p>
<p>TEJPAL B. BORKAR</p></div>Tejpal Borkartag:robotlegs.tenderapp.com,2009-10-18:Comment/272598622013-06-13T14:49:22Z2013-06-13T15:02:34ZRuntime Injection <div><blockquote>
<p>Thanks sir..Its absolutely correct sir.</p>
</blockquote>
<p>You’re welcome!<br>
Just for your information: I’m a woman. ;)</p>
<p>If you map your class like this:</p>
<p>injector.mapSingleton(SomeClass, "InjectChild");<br>
injector.mapSingleton(SomeClass, " InjectBase");</p>
<p>then you’ll have to inject it like this:</p>
<p>[Inject (name=“InjectChild“)] public var
child:SomeClass;</p>
<p>[Inject (name=“InjectBase“)] public var
base:SomeClass;</p>
<p>So, you’ll have 2 instances of SomeClass.</p>
<p>I don’t see how this would solve your problem.</p>
<p>You cannot use the „name“ inside of the Inject tag
for passing parameters!!!<br>
The name is used internally by the Injector. It’s kind of an
id that helps the Injector to differentiate between injection
points of the same type.</p>
<p>Using interfaces:</p>
<pre>
<code>injector.mapSingletonOf(IModel, AnotherModel, "anotherModel");
injector.mapSingletonOf(IModel, SomeModel, "someModel");</code>
</pre>
<p>then you can inject the interface like this:</p>
<pre>
<code>[Inject (name="someModel")]
public var someModel:IModel;//this would be an instance of SomeModel
[Inject (name="anotherModel")]
public var anotherModel:IModel;//this would be an instance of AnotherModel</code>
</pre>
<p>Another option would be to use the injector to retrieve an
instance of a mapped class, instead of injecting it:</p>
<pre>
<code>private var model:IModel;
if(someCondition)
model = injector.getInstance(IModel, "someModel");
else
model = injector.getInstance(IModel, "anotherModel");</code>
</pre>
<blockquote>
<p>but i just want to make very few changes in my existing
application.</p>
</blockquote>
<p>Well, I can’t see your code, so I can’t know what
exactly is to be done to solve your problem with the minimum amount
of effort and changes.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272598622013-06-21T10:13:53Z2013-06-21T10:13:53ZRuntime Injection <div><p>Feel free to reopen this discussion, if you're still having
problems. Please open new threads for new issues.</p></div>Ondina D.F.