Michel
Michel

Reputation: 23615

immutable collection?

i use a readonly collection to prevent users from altering my cached data, but the items themselves are ofcourse muatble. Is there a way to prevent that behaviour?

Michel

Upvotes: 4

Views: 239

Answers (3)

Ricky Helgesson
Ricky Helgesson

Reputation: 3586

One way is to have both an immutable and a mutable version of every business model object. I wrote an article on that here: http://rickyhelgesson.wordpress.com/2012/07/17/mutable-or-immutable-in-a-parallel-world/

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148524

you can set private constructor - so only inside items can create the instance.

Also , you can use the constructor to put readonly Values....

Upvotes: 0

Oded
Oded

Reputation: 499012

Either make the cached items immutable themselves, or pass through copies of the objects.

Upvotes: 6

Related Questions