Deleting entries from a collection

Posts   
 
    
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 08-Apr-2005 16:42:43   

Hi all,

I have the following to add entities to my entitycollection within an entity:

         Dim GroupStudent As New GroupStudentEntity

        GroupStudent.GroupIdent = _GroupEntity.GroupIdent
        GroupStudent.StudentIdent = CType(grdStudents.ActiveRow.Cells("StudentIdent").Value, Integer)

        _GroupEntity.GroupStudent.Add(GroupStudent)


When I call the adapter it adds the entries just fine.

Now let's say I want to delete some of those entities within the GroupStudent collection. Could I Remove them from the collection (that wouldn't work since the adapter just would not see them, right?) Is there a boolean like DeleteMe (I couldn't find one)? The only option I could think of is to create an array or collection of the the entities I want to delete and then go one by one and delete the entities. Is there a way to delete several entities with one delete sql statement?

Thanks,

Fishy

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 08-Apr-2005 20:53:12   

Fishy wrote:

Hi all,

I have the following to add entities to my entitycollection within an entity:

         Dim GroupStudent As New GroupStudentEntity

        GroupStudent.GroupIdent = _GroupEntity.GroupIdent
        GroupStudent.StudentIdent = CType(grdStudents.ActiveRow.Cells("StudentIdent").Value, Integer)

        _GroupEntity.GroupStudent.Add(GroupStudent)


When I call the adapter it adds the entries just fine.

Now let's say I want to delete some of those entities within the GroupStudent collection. Could I Remove them from the collection (that wouldn't work since the adapter just would not see them, right?) Is there a boolean like DeleteMe (I couldn't find one)? The only option I could think of is to create an array or collection of the the entities I want to delete and then go one by one and delete the entities. Is there a way to delete several entities with one delete sql statement?

Thanks,

Fishy

Hi, Fishy. There is no provision within the entity collections for having the "remove" action also perform a "delete". Also, as you suggested, performing a delete in the Adapter scenario isn't possible as "delete" isn't a behavior of the entity collections; it's located on the Adapter.

Your "delete bucket" is the only solution right now. I've requested a feature for the entity collections that would track changes to the collection (e.g., Deletes, Adds, etc) and allow you to pull out a UnitOfWork when you're ready to commit the changes.

Something like this:



entityCollection.StartTracking
entityCollection.Add(entity)
entityCollection.Remove(entity2)
uow = entityCollection.GetUnitOfWork()
adapter.Save(uow)


This would remove the necessity to have to maintain delete buckets and would further give you the ability to "roll back" changes to a collection if desired.

Jeff...

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 08-Apr-2005 21:21:37   

jeffreygg wrote:



entityCollection.StartTracking
entityCollection.Add(entity)
entityCollection.Remove(entity2)
uow = entityCollection.GetUnitOfWork()
adapter.Save(uow)


This would remove the necessity to have to maintain delete buckets and would further give you the ability to "roll back" changes to a collection if desired.

Jeff...

That would be sweet... sunglasses

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Apr-2005 11:54:54   

Fishy wrote:

Hi all,

I have the following to add entities to my entitycollection within an entity:

         Dim GroupStudent As New GroupStudentEntity

        GroupStudent.GroupIdent = _GroupEntity.GroupIdent
        GroupStudent.StudentIdent = CType(grdStudents.ActiveRow.Cells("StudentIdent").Value, Integer)

        _GroupEntity.GroupStudent.Add(GroupStudent)


When I call the adapter it adds the entries just fine.
Now let's say I want to delete some of those entities within the GroupStudent collection. Could I Remove them from the collection (that wouldn't work since the adapter just would not see them, right?) Is there a boolean like DeleteMe (I couldn't find one)? The only option I could think of is to create an array or collection of the the entities I want to delete and then go one by one and delete the entities. Is there a way to delete several entities with one delete sql statement?

As Jeff points out, you have to perform some sort of delete tracking yourself. This is by design, as deletes are considered irreversable actions which you should be performing with care. A unitofwork object could help you here.

The feature Jeff requested is complex though, as it requires a large versioning framework.

Frans Bouma | Lead developer LLBLGen Pro
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 09-Apr-2005 23:09:42   

I suppose you could extend an entity to include a isMarkDelete boolean. Then have something like


Public Shared sub DeleteCollection(byref collectionToDelete as entityCollection)
    For each entity as Entity in collectionToDelete
        If entity.isMarkDelete then
            if not entity.isNew then
                adapter.deleteentity(entity)
            endif
            collectionToDelete.Remove(entity)
         endif
    Next
end sub

Wouldn't that work?

(edit) of course having the adapter take in a collection and outputing one delete statement for all the pk's would be better I think.