CheckboxList - Adding/Removing from an EntityCollection (Adapter)

Posts   
 
    
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 01-Feb-2005 19:29:04   

I have a form that contains a checkboxlist that represents an EntityCollection of SpecialStatusEntities; the boxes that are selected represent the individual SpecialStatusEntities that are associated with a particular ApplicationEntity. I want an easy, efficient way of modifying the SpecialStatusEntities that are associated with the ApplicationEntity when the form is submitted. So I was thinking something along the lines of:


foreach(ListItem item in cblSpecialStatus.Items)
{
    if(item.Selected)
    {
        //call some add method of ApplicationEntity.SpecialStatusCollection to add 
        //SpecialStatusEntity to SpecialStatusCollection associated with this
        //ApplicationEntity
    }
    else
    {
        //call some removal method of ApplicationEntity.SpecialStatusCollection to
        //remove SpecialStatusEntity from SpecialStatusCollection associated with this
        //ApplicationEntity
    }

}

Any ideas on the best way to do this? I'd rather have to avoid creating a new SpecialStatusEntity object on each iteration and using that to remove the entity from the collection, but if that is the only way then I suppose that's the way to do it.

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 02-Feb-2005 09:48:47   

You can use a unitofwork for that: add all entities to remove to that unitofwork object using AddForDelete() and add the collection with the entities to add as AddCollectionForSave(), then commit the unitofwork.

Frans Bouma | Lead developer LLBLGen Pro
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 03-Feb-2005 19:24:27   

Otis wrote:

You can use a unitofwork for that: add all entities to remove to that unitofwork object using AddForDelete() and add the collection with the entities to add as AddCollectionForSave(), then commit the unitofwork.

I was looking at the documentation for the UnitOfWork2, and to be honest I'm not seeing how it applies to my current situation. Since the checkboxlist contains pk values for SpecialStatusEntities, I merely want to be able to call appEntity.SpecialStatusCollection.Remove(pk) or something like that.

The problem is, I think, that if a checkbox is selected, I have to first check if that particular status is already associated with the appEntity. If it is not, then I add it. The same goes with the unchecked items; if they exist then I remove them, otherwise I do nothing.

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

Your problem is more generic: you have a list of items to select for a particular parent item. The user starts checking and unchecking items. After that you have to update the existing list.

I always solve this the brute force way: in a transaction, first remove all existing entities for the list to check, then insert the newly checked items.

This is by far the most simplest way to do it.

Frans Bouma | Lead developer LLBLGen Pro