Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working on a collection / temporary object

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
I am looking for a pattern here. (Mediator maybe?) My intent is to allow a client to add-to, remove-from, and change objects in collection, with the option to cancel all changes and revert back to the original. The collection is owned by a parent object, which can be modified and then have the changes either saved or discarded.

I am thinking I need some kind of temporary collection, which is a clone of the "real" collection, so that I can perform modifications without affecting the original. Then, during the Save process, clear the items in the orginal collection and copy the items in the temporary object to the real object and persist to the database.

I am stuck on where to place the responsibilty for maintaining this temporary collection.

One twist, is that to avoid duplicating what is added, I'll need to be able to refer to the temporary collection so that I can filter the choices when listing the possible items to add.

Does this sound familiar to anyone?

Any thoughts on this much appreciated as always.

Dragonwell
 
Hi Dragon

The command pattern is usually the one that is recommended for supporting do/undo behaviour.

But it sounds like you don't really need a temporary collection, you can use the real one. You just need to make the changes in memory, and only persist them back to the DB (or not) at the end when you make your save/cancel decision.
 
I agree with stevexff, I think that command pattern is the better option for you.
 
Thanks. The problem is that I don't want to touch the object unless I know I am going to persist the changes. If the changes were simple, then maybe the command pattern with Do/Undo methods would work, but in the case of totally rearranging a collection it just seems too complicated to restore.

Simply nulling the object and re-syncing with the DB seems simple but I just can't seem to get that to work right because of existing references to it elsewhere in the app. (Maybe that's a bad sign...)

I ended up using a controller class between my Gui and the actual object. The controller maintains the temporaray collection for the gui to manipulate. During a Save, the temporaray collection is copied to the original, updating as needed.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top