tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2028-misunderstanding-the-postconstruct-metatagRobotlegs: Discussion 2013-03-25T18:33:24Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-02T15:37:52Z2013-03-02T15:37:52ZMisunderstanding the [PostConstruct] metatag<div><p>Take the <code>init()</code> out of the constructor (the
myCustomClass function) - this is causing it to run early. The
<code>[PostConstruct]</code> tag will ensure that the
<code>init()</code> function is run once injections are
complete.</p>
<p>hth,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-03T11:57:24Z2013-03-03T11:57:24ZMisunderstanding the [PostConstruct] metatag<div><p>Thanks for helping</p>
<p>If i take the <code>init()</code> function out of the
constructor, <code>init()</code> is never fired</p></div>poq2011tag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-03T22:09:01Z2013-03-03T22:10:36ZMisunderstanding the [PostConstruct] metatag<div><p>Hi poq2011,</p>
<p>If <code>init()</code> is not fired then your instance is not
being constructed by the Injector. How are you instantiating
<code>myCustomClass</code>?</p>
<p>BTW, it is very unusual (and a bit confusing) to have classes
that start with lowercase characters in AS3.
<code>myCustomClass</code> should really be
<code>MyCustomClass</code>. Instances on the other hand should
always start with lowercase characters. For example:</p>
<pre>
<code>var thing:Thing = new Thing();</code>
</pre>
<p>Anyhoo, for [Inject] or [PostConstruct] to work the instance
needs to be constructed by the Injector. If you are
<code>new'ing</code> it up yourself the Injector won't know
anything about it and thus won't be able to resolve any
dependencies or call the [PostContruct] method for you.</p>
<p>The Injector is a Factory. You give it a some configuration
information, and then you ask it to build objects for you. It
analyses the metadata in the class you are wanting to instantiate
and resolves dependencies recursively using the information you've
given it. But it's still just a Factory, it doesn't know anything
about objects that you haven't told it about or objects that it
didn't built itself.</p>
<p>Most of the things in the framework, like Mediators and
Commands, are constructed using the Injector, and that is why they
can have [Inject] and [PostConstruct] metadata tags.</p>
<p>So, if you tell the injector how to construct 'MyCustomClass`
and then inject that into something that is created by the injector
(like a Command) you should see things working correctly:</p>
<pre>
<code>// in your mapInjections() method
injector.mapSingleton(MyCustomClass);
// in a command or mediator
[Inject]
public var myCustomClass:MyCustomClass;</code>
</pre>
<p>Here we've told the injector what to do when someone needs an
instance of <code>MyCustomClass</code>. An instance of this class
won't be created until someone needs it. Because the
Command/Mediator needs it, and because that Command/Mediator is
created by the Injector, the Injector can create it, resolve it,
and inject it successfully.</p>
<p>Hope that clears things up a bit.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-04T08:04:44Z2013-03-04T08:04:44ZMisunderstanding the [PostConstruct] metatag<div><p>thanks that clarify the Injection mechanism.</p>
<p>However, if I understand well, I shouldn't need the
<code>[PostContruct]</code> metatag.</p>
<p>I want to inject <code>away3DView : MyAway3DView;</code> into a
custom class <code>MyCustomClass</code>.</p>
<p>So in my context class, I've configured the injector by
declaring</p>
<p><code>injector.mapValue(myAway3DView, view3D);</code></p>
<p>By doing so i should be able a get the instance of my class
MyAway3DView in any custom class constructed manually, no?</p>
<p><code>public class MyCustomClass{</code></p>
<pre>
<code> [Inject]
public var away3DView : MyAway3DView;</code>
</pre>
<p><code>}</code></p>
<p>but <code>away3DView</code> is always null. What's wrong?</p></div>poq2011tag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-04T10:20:08Z2013-03-04T10:20:08ZMisunderstanding the [PostConstruct] metatag<div><p>Hello,</p>
<p>By "always null" do you mean: null in the constructor?</p>
<p>A public property can not be set on an instance before that
instance has been constructed. This is what [PostConstruct] is for.
The instance needs to be constructed first, and then any properties
can be set, and then you need a way to know that all the properties
have been set. Please see:</p>
<p><a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/Common-Problems#wiki-null-in-constructor">
https://github.com/robotlegs/robotlegs-framework/wiki/Common-Proble...</a></p>
<p>Hope that helps</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-04T22:04:45Z2013-03-04T22:04:45ZMisunderstanding the [PostConstruct] metatag<div><p>Thanks but that didn't help.</p>
<p>Here is attached an example of what I'm trying to do. In short
:</p>
<blockquote>
<p>my AppContext maps an instance of
<code>MyClassToBeInjected</code> with
<code>injector.mapValue(MyClassToBeInjected,
myClassToBeInjectedInstance)</code>. This instance is added to
contextView.</p>
<p>I have a <code>ADummyView</code> mapped to
<code>ADummyViewMediator</code>. This component listens to stage
Mouse Click</p>
<p>I have a <code>MyServiceConstructingMyCustomClass</code> service
which is in charge of constructing an instance of
<code>MyCustomClass</code>.</p>
<p>The command <code>ConstructMyCustomClassCommand</code> is mapped
to the event <code>MyAppEvent.CONSTRUCT_CLASS</code> which is
dispatched by <code>ADummyViewMediator</code> when a click on stage
occurs. I want to get the injection of the instance of
<code>MyClassToBeInjected</code> in 'MyCustomClass()` when I
instanciate this class.</p>
</blockquote>
<p>How to achieve that? In my code, in <code>MyCustomClass()</code>
the injection of <code>myClassToBeInjectedInstance</code> is always
null and the <code>postConstruct</code> method is never fired even
if an injection rule (in this case
<code>injector.mapSingleton(MyCustomClass)</code> in the context )
is declared for this class.</p></div>poq2011tag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-04T22:12:37Z2013-03-04T22:12:37ZMisunderstanding the [PostConstruct] metatag<div><p>Hi. Could you post that as a ZIP? I don't have any tools for
unpacking RAR files.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-04T22:21:20Z2013-03-04T22:21:20ZMisunderstanding the [PostConstruct] metatag<div><blockquote>
<p>I have a MyServiceConstructingMyCustomClass service which is in
charge of constructing an instance of MyCustomClass.</p>
</blockquote>
<p>If you are constructing the MyCustomClass instances yourself
then the Injector won't know about them. You need to use the
injector to create the instances. You can do this like so:
<code>injector.getInstance(MyCustomClass)</code>.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-04T22:23:38Z2013-03-04T22:24:12ZMisunderstanding the [PostConstruct] metatag<div><p>BTW, do you need to use a service to manually create these
instance? Have you tried simply injecting
<code>MyCustomClass</code> into your mediator and seeing what
happens?</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-05T06:38:37Z2013-03-05T06:38:37ZMisunderstanding the [PostConstruct] metatag<div><p>Here is the zip file.</p>
<blockquote>
<p>If you are constructing the MyCustomClass instances yourself
then the Injector won't know about them. You need to use the
injector to create the instances. You can do this like so:
injector.getInstance(MyCustomClass).</p>
</blockquote>
<p>I want to inject 'myClassToBeInjectedInstance' when creating
'MyCustomClass', so it should be
'injector.getInstance(myClassToBeInjectedInstance)'<br>
How do i get access to the property 'injector'?</p>
<blockquote>
<p>Have you tried simply injecting MyCustomClass into your mediator
and seeing what happens?</p>
</blockquote>
<p>You mean tried to inject 'myClassToBeInjectedInstance' into the
meditaor. Yes I did, and it works as expected, null in the
constructor, but 'postConstruct' and 'onRegister' methods have
access to the injection.</p>
<blockquote>
<p>BTW, do you need to use a service to manually create these
instance?</p>
</blockquote>
<p>Yes Ithink so. I need to create manually objects depending on
data I've previously loaded on my Model and in response to a
specific event. What would you suggest?</p></div>poq2011tag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-05T09:43:11Z2013-03-05T09:43:11ZMisunderstanding the [PostConstruct] metatag<div><p>Hi. You can inject it into your service:</p>
<p>[Inject] public var injector:IInjector;</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-05T10:38:25Z2013-03-05T11:43:39ZMisunderstanding the [PostConstruct] metatag<div><p>Hi,</p>
<p>OK but I'd rather need the injector in
<code>MyCustomClass</code> in order to access
myClassToBeInjectedInstance via
<code>injector.getInstance(myClassToBeInjected)</code>.</p>
<p>Am I not going to run in the same problem since I still need to
do an injection, this time the injector itself??</p></div>poq2011tag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-05T14:28:28Z2013-03-05T14:28:28ZMisunderstanding the [PostConstruct] metatag<div><p>No, that's not what I mean. Inject the injector into your
service, and use it to create the MyCustomClass instances</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-05T19:53:20Z2013-03-05T19:53:20ZMisunderstanding the [PostConstruct] metatag<div><p>Thanks for helping!</p>
<p>Ok, it seems to work, but I don't understand why!</p>
<p>In my service, what the difference between this line of code</p>
<p><code>var temp : MyCustomClass =
injector.getInstance(MyCustomClass);</code> which will resolve all
injections in <code>MyCustomClass</code></p>
<p>and this line</p>
<p><code>var temp : MyCustomClass =new MyCustomClass;</code></p>
<p>which won'tresolve the injections in <code>MyCustomClass</code>
?</p>
<p>It seems quite magical to me...</p></div>poq2011tag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-06T23:22:33Z2013-03-06T23:22:33ZMisunderstanding the [PostConstruct] metatag<div><p>Hehe, it is the opposite of magical!</p>
<p>If this worked, it would be magical indeed:</p>
<pre>
<code>var temp : MyCustomClass = new MyCustomClass();</code>
</pre>
<p>In that case there is no system to analyse the Class metadata
and perform any processing.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/255067872013-03-25T11:21:17Z2013-03-25T11:21:17ZMisunderstanding the [PostConstruct] metatag<div><p>I guess this is resolved, right? I’m closing this
discussion for now. You can re-open it, if need be.</p></div>Ondina D.F.