Adding to ReadOnlyList Collection

Adding to ReadOnlyList Collection

Old forum URL: forums.lhotka.net/forums/t/3765.aspx


rbellow posted on Tuesday, October 23, 2007

I am reading a file and creating 100s of objects at a time. I am looping through a ReadOnlyList of existing items to check for duplicates. If there are no duplicates I add another item. I dont want to have to pull down a new ReadOnlyList from the database to continue checking for duplicates. Is there a way to add a new item to the ReadOnlyList temporarily. I don't want to do any writes to the database, I just want to include the new ObjectInfo item in the collection. Is this possible?

Marjon1 replied on Tuesday, October 23, 2007

This can be done, by creating your own add method to the class and setting ReadOnly to false and then back to true again when done, just like you do as part of the normal fetch.

The other option, depending on the type of object that you are creating is to take a look at one of the samples that Rocky posted recently where saving an editable root object would fire an event which the readonly list could subscribe to and then update itself.

You wouldn't need to worry about adding the item yourself, and would prevent others from doing so as well. It's something rocky was going to put in his book, but I can't find it at the moment.

DavidDilworth replied on Wednesday, October 24, 2007

See the example code in this post to see an example of changing a ReadOnlyList to perform editing operations.

david.wendelken replied on Wednesday, October 31, 2007

rbellow:

I am reading a file and creating 100s of objects at a time. I am looping through a ReadOnlyList of existing items to check for duplicates.

Not a direct answer to your question, but this might be useful to consider.

The algorithm you described runs slower and slower the more objects there are in the file. 
This is because each object has to loop thru all the objects that came before it.

You should consider another algorithm, like attaching a sorted collection to it.  That way (presumably) the sorted list can check for the existence much faster. 

The CSLA Contrib class ObjectListView might work nicely, as might the CSLA class SortedBindingList.  Haven't had to use either for this purpose as I get most of my data from a SQL database and I can solve the problem using SQL.

 

Copyright (c) Marimer LLC