JimFoye wrote:
My POEnity class has a related collection PODetail, which I prefetch. I need to delete one of the detail items. How do I flag it so that when I call SaveEntity(..., true) the detail record gets deleted from the db? Or is this not going to be so elegant? Boy, I miss the self-servicing so much already.
Hi, Jim. It's funny, but we're both working on Purchase Order systems right now.
Regarding your question; UnitOfWork is your friend here as you can create a PODetailDeleteBucket to which you add PODetail entities that you want deleted, then you add that bucket to the UnitOfWork using UnitOfWork.AddCollectionForDelete.
The problem is that it's a bit of a pain tracking what's getting deleted, etc. So, I've built a "State Machine" that basically tracks actions made against collections for you and does the heavy lifting. It's more oriented toward datarows/tables as most of my subcollections are rendered as datatables to deal with multi-entity display issues, but it could be easily adapted for use directly with entities.
Basically, you instantiate the machine and provide the datasource. Then, you tell it to start tracking and from then on whatever you add, update, or delete from the machine automatically gets tracked and can be pulled out as collections (datarows, in my case) independently. Here's some code:
Dim detailStateMachine as StateMachine
Dim detailTable as DataTable
Dim uow as UnitOfWork2
'Instantiation
detailTable = _service.GetPODetailByPOID(pOID)
detailStateMachine = new StateMachine(detailTable)
uow = New UnitOfWork2
'Instructs the machine that all actions taken heretofore must be tracked
detailStateMachine.StartTracking()
'Pseudo-code follows
detailStateMachine.Add(key, data)
detailStateMachine.Update(key, data)
detailStateMachine.Delete(key)
'Data in each "State" can be pulled out independently
uow.AddCollectionForDelete(detailStateMachine.GetData(StateType.ToDelete))
uow.AddCollectionForSave(detailStateMachine.GetData(StateType.ToAdd))
uow.AddCollectionForSave(detailStateMachine.GetData(StateType.Persisted))
uow.Commit(myAdapter,True)
If you want the code, let me know. I've found that it works fantastically and my grids and sub-collections management dev time has shrunk dramatically - and I really hate managing all of that stuff manually, if you know what I mean.
Jeff...
<Edit> Changed two of the .AddCollectionForDelete() commands to .AddCollectionForSave()