tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/13280-mixing-injection-with-registerclassalias-overrides-di-and-creates-new-instanceRobotlegs: Discussion 2018-10-18T16:35:58Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/386121912015-12-04T13:07:21Z2015-12-05T18:27:37ZMixing injection with 'registerClassAlias' overrides DI and creates new instance?<div><p>Hi,</p>
<blockquote>
<p>Also if I add a trace to the constructor it definitely gets
created twice. I think that's due to the
registerClassAlias(UserProfileModel, "models.UserProfileModel") but
not 100% sure on that.</p>
</blockquote>
<p>Yes, that's right, it only gets created twice when a
registerClassAlias is used or a metatag like<br>
[RemoteClass(alias=" UserProfileModel")] inside of UserProfileModel
<em>and</em> when there is already some data stored on disk. I just
tested your use case: in the flash builder profiler one instance of
UserProfileModel holds the values saved on disk and the other
instance is the one instantiated by the injector.<br>
Nevertheless, in my test the UserProfileModel, injected in
different other classes as well, gets updated and shows the same
values after reading data from the shared object.</p>
<p>There will be only one instance of your model, if you map the
model like shown in this discussion:<br>
<a href="http://knowledge.robotlegs.org/discussions/questions/769-question-about-injecting-data-from-disk#comment_12652442">
http://knowledge.robotlegs.org/discussions/questions/769-question-a...</a></p>
<p>For robotlegs 2 the mapping would look like this:</p>
<pre>
<code>var profileModel: UserProfileModel;
var sharedObject:SharedObject = SharedObject.getLocal("application-name");
injector.map(SharedObject).toValue(sharedObject);
if (sharedObject.data.profileModel != null)
{
profileModel = sharedObject.data.profileModel;
injector.map(UserProfileModel).toValue(profileModel);
}
else
{
injector.map(UserProfileModel).asSingleton();
}</code>
</pre>
<p>It works well on my end.</p>
<p>Another way of solving this would be to use some kind of object
introspection (ObjectUtil, describeType, as3commons-reflect)<br>
to set model's data without having to know each property's name Or,
you could use JSON to stringify(sharedObject.data) and then parse
it inside your model's method to set its properties. That works
well for private properties as well.</p>
<p>Also, I'm mentioning the IExternalizable as a possible solution
to a use case like yours. I never used it, so I can't say much
about it.</p>
<p>Hope that helps.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/386121912015-12-04T18:25:32Z2015-12-05T18:27:36ZMixing injection with 'registerClassAlias' overrides DI and creates new instance?<div><p>That helps loads Ondina!<br>
I have set it up as in your example above and it does indeed work
without<br>
creating multiple instances of the model.<br>
It means I can re-assign my object using registerClassAlias, though
it's<br>
probably best practise to use object introspection (especially for
nested<br>
objects) but this certainly solves this problem for me.<br>
Thanks you guys for all the work on RobotLegs btw, I've not posted
on here<br>
for ages but myself and a colleague have used it on several AIR
x-platform<br>
mobile apps and it's been really useful at organising the app's<br>
architecture. The above method of mapping the model with the disk
data will<br>
help get things cleaner too.</p>
<p>Cheers<br>
Sean</p></div>Sean Duffytag:robotlegs.tenderapp.com,2009-10-18:Comment/386121912015-12-04T18:26:16Z2015-12-05T18:27:36ZMixing injection with 'registerClassAlias' overrides DI and creates new instance?<div><p>That helps loads Ondina!<br>
I have set it up as in your example above and it does indeed work
without creating multiple instances of the model.<br>
It means I can re-assign my object using registerClassAlias, though
it's probably best practise to use object introspection (especially
for nested objects) but this certainly solves this problem for
me.<br>
Thanks you guys for all the work on RobotLegs btw, I've not posted
on here for ages but myself and a colleague have used it on several
AIR x-platform mobile apps and it's been really useful at
organising the app's architecture. The above method of mapping the
model with the disk data will help get things cleaner too.</p>
<p>Cheers<br>
Sean</p></div>seanjamesduffytag:robotlegs.tenderapp.com,2009-10-18:Comment/386121912015-12-05T18:26:47Z2015-12-05T18:27:36ZMixing injection with 'registerClassAlias' overrides DI and creates new instance?<div><p>Hey Sean,</p>
<p>I'm glad to hear that the mapping-example solved your problem
and also that you and your colleagues are enjoying using
robotlegs:)</p>
<p>I'm marking this discussion as resolved (closing it), but feel
free to re-open it or to create new discussions for other
questions, if need be.</p>
<p>See you!<br>
Ondina</p></div>Ondina D.F.