tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/968-passing-mutable-objects-through-eventsRobotlegs: Discussion 2012-07-22T15:40:03Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/174981422012-07-22T13:18:21Z2012-07-22T13:18:22ZPassing mutable objects through events<div><p>Hi, I'm new to this whole MVC(s) stuff, just migrated from
PureMVC + Fabrication, and I must tell You, that it was quite
easy!</p>
<p>The code's much cleaner, type-safe, easy to write (no
boilerplate code), the injector is awesome, and the framework
enforces proper decoupled structure (previously I had logic in
mediators, and proxies referencing each other directly) - all of
this made me think if passing mutable objects like Vectors from
model to view is a good idea.</p>
<p>Simplifying the case a little bit, I have a vector of Message
objects, that I pass from Channel model to Chat view. As it's not
an immutable value, I'm just passing a pointer to the values, and
so the view could change the data of Model directly. I could make a
shallow copy of the vector, but still, the Chat will be able to
change specific Messages.</p>
<p>I'm not afraid of the scenario to actually happen, but the
question remains: Is passing mutable objects a terrible manner for
MVC?</p>
<p>Right now I'm thinking of making the Message properties
read-only, that could be set only in constructor, and use shallow
copies of vectors - is it a right solution, or I shouldn't even
bother?</p>
<p>Thanks, Robotlegs team!</p></div>Markustag:robotlegs.tenderapp.com,2009-10-18:Comment/174981422012-07-22T13:35:48Z2012-07-22T13:35:48ZPassing mutable objects through events<div><p>Hey Markus,</p>
<p>I'm very glad to hear you're enjoying the framework!</p>
<p>Regarding your question, I agree that in theory, values passed
to the<br>
view should be immutable as far as the model is concerned.</p>
<p>I also think, however, that in most cases you shouldn't
bother,<br>
really. While passing read-only structures or deep copies around
will<br>
prevent lazy people (like Future You) from taking shortcuts,
they're<br>
pretty likely to find ways around that anyway, if they really try.
If<br>
you pass copies around, for example, they might change the data<br>
nevertheless, because the effect will be seen in a view that
handles<br>
the data after where they're currently handled.</p>
<p>What I mean is that even in large systems, you should use a
mixture of<br>
technical enforcement and conventions to get to a clean system.</p>
<p>If, however, your views are not to be trusted for some
reason<br>
(remember that pest of a coworker who's working on them, Future
You: he is so goddamn prone to take the most awful shortcuts!),
then you<br>
should take technical precautions to whatever degree seems sensible
to<br>
you.</p>
<p>Luckily, some of the worst shortcuts you ever encountered where
taken<br>
by that other guy, Past You, and he doesn't work on the project<br>
anymore.</p></div>Till Schneidereittag:robotlegs.tenderapp.com,2009-10-18:Comment/174981422012-07-22T15:12:09Z2012-07-22T15:12:09ZPassing mutable objects through events<div><p>Haha, thanks for the great answer! I'm really happy, that after
encountering another, random problem in fabrication, I decided to
rather switch framework then fix it again.</p></div>Markustag:robotlegs.tenderapp.com,2009-10-18:Comment/174981422012-07-22T15:40:02Z2012-07-22T15:40:02ZPassing mutable objects through events<div><p>Glad to be of help :)</p></div>Till Schneidereit