This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm looking for suggestions on how to implement an EntityCollection property as read only. Even with only a getter defined, the things that are referenced are still read-write. I am trying to restrict my collections to read-only, so I can manage the addition and deletion through my own Add/Remove methods. A ReadOnlyEntityCollection sounds logical, but it doesn't appear to exist. Is there a simple way to create my own? The other option I can think of is to return an array using Get(x).ToArray(), which does work, but is not quite as elegant.
Thanks! Brad
|
|
|
There are (if I've read you correctly) a couple of different issues here. First, preventing people from modifying the set of elements in the collection (i.e. suppressing Add and Remove). We don't currently have a mechanism for this, though I'm sure we could add an AsReadOnly() method that returned a ReadOnlyCollection<T> (a la List<T>.AsReadOnly) if required. However, you say you want to do this so that you can manage addition and deletion through your own Add/Remove methods. Depending on what you are trying to do, a better strategy may be to override InsertItem and RemoveItem, which are called by Add and Remove. Second, you comment that "the things which are referenced are still read-write." I may have misread you here -- are you concerned that the items in the collection can be modified? E.g. if the collection contains a Person, then anybody with a reference to the collection can modify that Person, and you don't want that? Unfortunately this is pretty much impossible to avoid, because of the nature of .NET reference types and the lack of a C++-style const modifier. The only workaround for this is really manually creating a ReadOnlyPerson type and copying the data into it, which is impractical for most scenarios unfortunately... |
|