UnitofWork CommitOrder issue

Posts   
 
    
Heather
User
Posts: 17
Joined: 16-Jan-2009
# Posted on: 24-Feb-2009 21:48:52   

v2.6.9.116, sql server, vb.net 2008, adapter template

I am trying to set the CommitOrder on a unit of work but the CommitUnitOfWork seems to ignore it. Here is code from my manager class.

Public Shared Sub InsertDocument(ByVal equipmentId As Integer, ByVal document As DocumentEntity, ByVal isDefault As Boolean)
      Dim eq As New EquipmentDocumentEntity()
      eq.EquipmentId = equipmentId
      eq.IsDefault = isDefault
      eq.Document = document
      Dim uow As New UnitOfWork2()
      uow.AddForSave(eq)
      uow.AddForSave(document)
      If isDefault Then
        Dim bucket As New RelationPredicateBucket()
        bucket.PredicateExpression.Add(New FieldCompareValuePredicate(EquipmentDocumentFields.EquipmentId, Nothing, ComparisonOperator.Equal, equipmentId))
        bucket.PredicateExpression.Add(New FieldCompareValuePredicate(EquipmentDocumentFields.IsDefault, Nothing, ComparisonOperator.Equal, True))
        Dim defdoc As New EquipmentDocumentEntity
        defdoc.IsDefault = False
        uow.AddUpdateEntitiesDirectlyCall(defdoc, bucket)
        uow.CommitOrder.Add(UnitOfWorkBlockType.UpdatesPerformedDirectly)
        uow.CommitOrder.Add(UnitOfWorkBlockType.Inserts)
      End If
      CommitUnitOfWork(uow)
    End Sub

The Inserts still get called before my UpdateEntitiesDirect calls. I tried changing the order that I added to the UnitOfWork and still no dice. What am I doing wrong?

Heather
User
Posts: 17
Joined: 16-Jan-2009
# Posted on: 24-Feb-2009 21:53:10   

Just for clarity... the CommitUnitOfWork call exists in the base class but it is as follows:

    
    Protected Shared Function GetAdapter() As IDataAccessAdapter
      Return New DataAccessAdapter()
    End Function

    Protected Shared Function CommitUnitOfWork(ByVal batch As UnitOfWork2) As Integer
      Using da As IDataAccessAdapter = GetAdapter()
        Return CommitUnitOfWork(batch, da)
      End Using
    End Function

    Protected Shared Function CommitUnitOfWork(ByVal batch As UnitOfWork2, ByVal adapter As IDataAccessAdapter) As Integer
      Return batch.Commit(adapter, True)
    End Function
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 24-Feb-2009 22:12:31   

You need to pass a list of the UnitOfWorkBlockTypes to the constructor of the UoW. When you use .Add you are appending them to the existing default list, and as the list already contains all the items this has no effect on the order...

Matt

Heather
User
Posts: 17
Joined: 16-Jan-2009
# Posted on: 24-Feb-2009 22:21:57   

OK, thank you, works much better now. Also seems that I can do a uow.commitorder.clear then the adds (so I don't have to create a new collection object and add the block types etc). Any drawbacks to that?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 24-Feb-2009 22:37:54   

Should work fine as well.

Matt