tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/12859-injections-subclassing-and-singletonsRobotlegs: Discussion 2015-06-24T13:35:21Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/365862662015-04-15T12:05:42Z2015-04-15T12:05:43ZInjections, Subclassing, and Singletons<div><p>Hi,</p>
<p>In have 2 model classes: Model and ExtendedModel. Some commands
require Model and other, more specialized ones, require
ExtendedModel. I'd like Model to be a singleton and that the same
instance be used by all commands, whether they expect Model or
ExtendedModel. So this is how I configured it:</p>
<pre>
<code> injector.map(ExtendedModel).asSingleton();
injector.map(Model).toSingleton(ExtendedModel);</code>
</pre>
<p>The problem is that 2 different instances are injected.</p>
<p>in command A:</p>
<pre>
<code> [Inject]
public var model:Model;</code>
</pre>
<p>In command B:</p>
<pre>
<code> [Inject]
public var extendedModel:ExtendedModel;</code>
</pre>
<p>How can I change the configuration so that model ==
extendedModel ?</p>
<p>Thanks.</p></div>yishayjobstag:robotlegs.tenderapp.com,2009-10-18:Comment/365862662015-04-16T12:57:31Z2015-04-16T12:57:31ZInjections, Subclassing, and Singletons<div><p>Hi,</p>
<blockquote>
<p>In have 2 model classes: Model and ExtendedModel.</p>
</blockquote>
<p>That means that Model is the superclass and ExtendedModel the
subclass, right?<br>
So, ExtendedModel <strong>extends</strong> Model?</p>
<blockquote>
<p>I'd like Model to be a singleton and that the same instance be
used by all commands, whether they expect Model or
ExtendedModel.</p>
</blockquote>
<p>Why would you want that? If a class needs ExtendedModel, why
would you give it a superclass?</p>
<blockquote>
<p>How can I change the configuration so that model ==
extendedModel ?</p>
</blockquote>
<p>Does this mean that you want the superclass to behave like the
subclass? If so, then what's the purpose of subclassing? A subclass
should inherit all the methods of a superclass, override
superclass' methods if need be, and perhaps have some additional
methods of its own.</p>
<p>I guess, that your intention is to inject the Superclass
somewhere, change its properties, and then when you inject the
Subclass somewhere else, you're expecting that the Subclass'
properties reflect the changes in the Superclass. But, that's not
possible. Changes made in superclasses are not reflected in the
subclasses, or vice versa.</p>
<p>Forget about injection and mappings for a moment, and just try
out a simple example:<br>
Create a SomeSuperClass with getters and setters for someProperty.
Set it to a default value of, say, "Super".<br>
Create another class, SomeSubClass, that extends
SomeSuperClass.</p>
<p>Then somewhere in your code instantiate the claasses:<br>
var someSuper:SomeSuperClass = new SomeSuperClass();<br>
var someSub:SomeSubClass = new SomeSubClass();</p>
<p>Set someSuper.someProperty to a new value, and trace out the
results for someSuper.someProperty and someSub.someProperty. You'll
see that someSub.someProperty will not change.</p>
<p>This mapping<br>
injector.map(Model).toSingleton(ExtendedModel); doesn't make sense
for your use case.</p>
<p>I guess you've seen this kind of mapping used for
interfaces:<br>
injector.map(ISomeModel).toSingleton(SomeModel);</p>
<p>But a superclass doesn't behave like an interface.</p>
<blockquote>
<p>Some commands require Model and other, more specialized ones,
require ExtendedModel.</p>
</blockquote>
<p>Then you need 2 mappings:</p>
<p>injector.map(Model).asSingleton();</p>
<p>injector.map(ExtendedModel).asSingleton();</p>
<p>In case I misunderstood your question or your use case, please
try to explain in more detail what you need to happen with your
Model and ExtendedModel when you inject them into other
classes.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/365862662015-04-16T13:52:38Z2015-04-16T13:52:38ZInjections, Subclassing, and Singletons<div><p>Hi Ondina,</p>
<p>Thanks so much for responding.</p>
<p>Yes, ExtendedModel extends Model. My application's behavior is
configurable. Depending on the configuration some events, and thus
some commands, will fire and some will not. Some commands will be
executed regardless of the configuration. I'd like the singleton
model to reflect the different configurations.</p>
<p>Consider the following setup:</p>
<p>Model, ConfigAExtendedModel, ConfigBExtendedModel,
CongfigACommand, ConfigBCommand, AnyConfigCommand.</p>
<p>In configuration A:</p>
<p>ConfigACommand -> ConfigAExtendedModel<br>
AnyConfigCommand -> Model</p>
<p>in configuration B:</p>
<p>ConfigBCommand -> ConfigBExtendedModel<br>
AnyConfigCommand -> Model</p>
<p>AnyConfigCommand can be used in both configurations because it
relies only on the model's basic API which is represented in
Model.</p>
<p>I've worked around the problem by doing</p>
<pre>
<code> var extendedModel:ExtendedModel = new ExtendedModel();
injector.map(ExtendedModel).toValue(extendedModel);
injector.map(Model).toValue(extendedMode);</code>
</pre>
<p>but that I don't get lazy instantiation.</p>
<p>I hope that makes my problem more clear.</p>
<p>Thanks.</p></div>yishayjobstag:robotlegs.tenderapp.com,2009-10-18:Comment/365862662015-04-16T15:35:53Z2015-04-16T15:35:53ZInjections, Subclassing, and Singletons<div><p>No problem.</p>
<p>Thanks for providing more details.<br>
Interesting workaround. If it works for you, all is good:) I can't
think of anything better than your solution, right now.</p>
<p>Just in case you want a class to have its dependencies satisfied
by the injector, you should let the injector instantiate it. So,
instead of new ExtendedModel() do this:</p>
<p>var extendedModel: ExtendedModel =
injector.getOrCreateNewInstance(ExtendedModel);</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/365862662015-04-19T07:22:55Z2015-04-19T07:22:55ZInjections, Subclassing, and Singletons<div><p>Sounds good.</p></div>yishayjobs