tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/497-updating-injected-valueRobotlegs: Discussion 2012-03-03T02:41:28Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T11:08:15Z2012-03-02T11:09:16ZUpdating injected value<div><p>There are no "live" injections in Robotlegs (or
SwiftSuspenders). In other words, injected values will not change
after injection even if the mapping changes, only new injections
will receive the new values.</p>
<p>But it's generally not advised to map "primitives" into the
injector from a design perspective anyway (they are too fine
grained to be "flattened" into a container).</p>
<p>In your situation I would make a model called UserModel. That
model would have a property called "userId". When the property
changes (via the model's setter) the model would dispatch an event
to inform any mediators that the value has changed.</p>
<p>You would map the UserModel as a singleton at startup and inject
it into any mediators that need access to it. Those mediators would
register listeners so that they can be informed when the property
changes.</p>
<p>Hope that helps!</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T11:15:05Z2012-03-02T11:15:05ZUpdating injected value<div><p>Thanks, Shaun. That UserModel approach is what I originally
implemented but then I thought, "Hey, injecting user ID would look
neater." But okay, I'll go back to having UserModel.</p></div>Omer Hassantag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T11:25:43Z2012-03-02T11:25:43ZUpdating injected value<div><p>Perhaps, but a plain string doesn't give you much control or
protection. Having a proper model allows you to encapsulate the
information properly and guard access to it.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T17:20:14Z2012-03-02T17:20:14ZUpdating injected value<div><p>Several of my services also require user ID but I don't know if
it would be a good idea to have them depend upon model. I usually
try to make services independent of the rest of the application
which allows me to use them in multiple projects. I would love to
hear your thoughts on this.</p></div>Omer Hassantag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T17:36:45Z2012-03-02T17:36:45ZUpdating injected value<div><p>You're right that the services should not depend on the
UserModel, but they should not depend (statefully) on userIds
either. Surely those services would have methods that accept
userIds as parameters rather than actually depending on userIds?
Something like <code>service.loadContacts(userId)</code>. Services
should be stateless.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T18:25:27Z2012-03-02T18:25:27ZUpdating injected value<div><p>Well, these services extend a subclass of
mx.rpc.remoting.RemoteObject class, so they aren't entirely
stateless to begin with. That subclass has a public property
<code>userId</code>. All the remote methods require user ID as
their first argument and I am using the
<code>RemoteObject.convertParametersHandler</code> property to
prepend <code>userId</code> to all argument lists of remote method
calls. So this service doesn't require me to explicitly pass userId
to its methods like <code>service.loadContacts(userId)</code> but
instead allows me to set its <code>userId</code> property and call
its methods without having to specify user ID every time. This is
why I was thinking of injecting user ID directly into my service
class' <code>userId</code> property. Please point out any design
blunders I am making here. Thanks!</p></div>Omer Hassantag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T18:59:21Z2012-03-02T18:59:21ZUpdating injected value<div><p>In that case, instead of injecting the userId via metadata,
perhaps just expose a userId setter on your abstract service class.
When the userId property changes on the UserModel an event is
dispatched that is bound to a command. That command can have the
UserModel injected into it and can set the userId property on each
service.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T19:13:49Z2012-03-02T19:13:49ZUpdating injected value<div><p>Thanks for the nice solution, Shaun, and for taking the time to
discuss this with me.</p></div>Omer Hassantag:robotlegs.tenderapp.com,2009-10-18:Comment/141707842012-03-02T20:06:08Z2012-03-02T20:06:08ZUpdating injected value<div><p>It's a pleasure! Hope it works out nicely. Feel free to close
this ticket if you are satisfied.</p></div>Shaun Smith