*Urgent* Need help adding/removing Entities from EntityCollection

Posts   
 
    
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 16-Feb-2005 16:10:13   

I am having difficulty adding/removing items from an EntityCollection. Scenario is as follows:

table structure

APPLICATION ApplicationId PK

BRANCH_OF_SERVICE BranchOfServiceId PK

--and the link table OTHER_SERVICE BranchOfServiceId FK ApplicationId FK OtherServiceId PK

So in a form I have a CheckBoxList bound to BranchOfServiceEntityCollection that I want to iterate through, and if the box is checked I want to create a new OtherServiceEntity (with the selected BranchOfServiceEntity being the related entity) and add it to the ApplicationEntity.OtherServiceEntityCollection if the OtherServiceEntityCollection does not already contain an OtherServiceEntity with the BranchOfServiceId equal to the selected checkbox item's value.

I see numerous methods of doing this, but I am unsure of how to do it. UnitOfWork has been mentioned but in the form, the user might be filling out a new application, hence creating a new ApplicationEntity, so what is the easiest way to add and remove from the OtherServiceEntityCollection? Please help--deadline approaching stuck_out_tongue_winking_eye

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 16-Feb-2005 19:16:09   

You want to save on the fly or once , when the user clicks OK on the form?

either way, I don't really understand what the real problem is, as you could add the entities to the ApplicationEntity.OtherServiceEntityCollection and when OK is clicked, save ApplicationEntity, or do I miss something?

Frans Bouma | Lead developer LLBLGen Pro
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 16-Feb-2005 22:16:35   

Otis wrote:

You want to save on the fly or once , when the user clicks OK on the form?

either way, I don't really understand what the real problem is, as you could add the entities to the ApplicationEntity.OtherServiceEntityCollection and when OK is clicked, save ApplicationEntity, or do I miss something?

No, you understand it--I want to save it when the user clicks the Save button. But my real confusion was that I have seen in numerous places on these forums that just removing entities from a collection or adding does not mean that it will be persisted to the database. But I assume this to mean that you still have to explicitly call some Save() method, whether it be Save() on the entity containing the collection (using Adapter pattern, btw) , using a UnitOfWork2, or saving the entity collection directly. My other question deals with the best way to add an existing entity to the collection. Since the checkboxlist is bound to BranchOfServiceCollection, can I get a BranchOfService entity directly from the SelectedItem in the CheckboxList, and do something like:


OtherServiceEntity otherService = new OtherServiceEntity();
otherService.BranchOfService = (BranchOfService)BranchOfServiceCheckboxList.SelectedItem;
appEntity.OtherServiceEntityCollection.Add(otherService);

adapter.SaveEntity(appEntity);

Would that work?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Feb-2005 10:48:18   

Because removing an entity from a collection is an ambiguistic action (it can mean different things), you have to tell the code what you want to do. This is done best with keeping track of what you want to do using a unitofwork object, or by keeping a different collection around in which you store the entities to delete.

This way you can then save the objects in one go at the end of the form, or simply throw away the tracked objects when the user clicks cancel (that's why it's important actions are done at the end! simple_smile )

It's up to you how you track this, there are various options available, like a unitofwork object or a dummy collection. Also use the option to save field values inside an entity if you want to roll back changes when the user clicks cancel.

The work you should do behind the form should be targeted towards building up the information to send to the persistence layer for processing. So adding an entity to another entities' collection will make them related to each other and as it is an existing entity, FK's will by synchronized. If you want to undo that situation later on, because the user clicked cancel, you have to undo that assignment as well, there is no rollback feature there (which would be nice, but is very complex to implement and use, that's why it's not there (yet))

Frans Bouma | Lead developer LLBLGen Pro