On 10/11/2009, at 7:57 PM, Dirk Olmes wrote:
Lachlan Deck wrote:
Hi there,
given some stuff we've seen in our own code (and general better
practices for dealing with collections in general) I thought I'd
suggest
the following changes for the default cayenne entity templates:
- private ivars rather than protected (if they're not already)
- return Collections.unmodifiableList(foo) rather than returning the
actual list that's modified via addTo/removeFrom etc.
Thoughts? Philosophies?
There's actually two schools of thoughts here:
- lock down access to the collections as you propose. However, given
that you cannot detect if a Collection is immutable or not (thanks to
Collections' broken interface design, think of NSArray vs
NSMutableArray) this is only a weak workaround.
True.
- implement custom collections that are ObjEntity aware and "do the
right thing" when touched.
I haven't looked at the Cayenne source for quite some time now but
IIRC
the code currently does the second option above ...
Right. They are...but this (I believe) is too easily exposed to user
code. e.g.,
for (Bar relation : foo.allBars()) {
if (some condition) {
foo.removeFromAllBars(relation); // will throw exception
}
}
with regards,
--
Lachlan Deck