@@ -532,25 +532,7 @@ public void Sort()
532
532
// the entity associated with the current action.
533
533
var currentEntity = action . Instance ;
534
534
535
- int batchNumber ;
536
- if ( _latestBatches . ContainsKey ( entityName ) )
537
- {
538
- // There is already an existing batch for this type of entity.
539
- // Check to see if the latest batch is acceptable.
540
- batchNumber = FindBatchNumber ( action , entityName ) ;
541
- }
542
- else
543
- {
544
- // add an entry for this type of entity.
545
- // we can be assured that all referenced entities have already
546
- // been processed,
547
- // so specify that this entity is with the latest batch.
548
- // doing the batch number before adding the name to the list is
549
- // a faster way to get an accurate number.
550
-
551
- batchNumber = _actionBatches . Count ;
552
- _latestBatches [ entityName ] = batchNumber ;
553
- }
535
+ var batchNumber = GetBatchNumber ( action , entityName ) ;
554
536
_entityBatchNumber [ currentEntity ] = batchNumber ;
555
537
AddToBatch ( batchNumber , action ) ;
556
538
}
@@ -567,23 +549,31 @@ public void Sort()
567
549
}
568
550
}
569
551
570
- /// <summary>
571
- /// Finds an acceptable batch for this entity to be a member as part of the <see cref="InsertActionSorter" />
572
- /// </summary>
573
- /// <param name="action">The action being sorted</param>
574
- /// <param name="entityName">The name of the entity affected by the action</param>
575
- /// <returns>An appropriate batch number; todo document this process better</returns>
576
- private int FindBatchNumber ( EntityInsertAction action , string entityName )
552
+ private int GetBatchNumber ( EntityInsertAction action , string entityName )
577
553
{
578
- // loop through all the associated entities and make sure they have been
579
- // processed before the latest
580
- // batch associated with this entity type.
581
-
582
- // the current batch number is the latest batch for this entity type.
583
- var latestBatchNumberForType = _latestBatches [ entityName ] ;
554
+ int batchNumber ;
555
+ if ( _latestBatches . TryGetValue ( entityName , out batchNumber ) )
556
+ {
557
+ // There is already an existing batch for this type of entity.
558
+ // Check to see if the latest batch is acceptable.
559
+ if ( IsProcessedAfterAllAssociatedEntities ( action , batchNumber ) )
560
+ return batchNumber ;
561
+ }
562
+
563
+ // add an entry for this type of entity.
564
+ // we can be assured that all referenced entities have already
565
+ // been processed,
566
+ // so specify that this entity is with the latest batch.
567
+ // doing the batch number before adding the name to the list is
568
+ // a faster way to get an accurate number.
569
+
570
+ batchNumber = _actionBatches . Count ;
571
+ _latestBatches [ entityName ] = batchNumber ;
572
+ return batchNumber ;
573
+ }
584
574
585
- // loop through all the associations of the current entity and make sure that they are processed
586
- // before the current batch number
575
+ private bool IsProcessedAfterAllAssociatedEntities ( EntityInsertAction action , int latestBatchNumberForType )
576
+ {
587
577
var propertyValues = action . State ;
588
578
var propertyTypes = action . Persister . ClassMetadata . PropertyTypes ;
589
579
@@ -592,23 +582,20 @@ private int FindBatchNumber(EntityInsertAction action, string entityName)
592
582
var value = propertyValues [ i ] ;
593
583
var type = propertyTypes [ i ] ;
594
584
595
- if ( type . IsEntityType && value != null )
585
+ if ( type . IsEntityType &&
586
+ value != null )
596
587
{
597
588
// find the batch number associated with the current association, if any.
598
589
int associationBatchNumber ;
599
- if ( _entityBatchNumber . TryGetValue ( value , out associationBatchNumber ) && associationBatchNumber > latestBatchNumberForType )
590
+ if ( _entityBatchNumber . TryGetValue ( value , out associationBatchNumber ) &&
591
+ associationBatchNumber > latestBatchNumberForType )
600
592
{
601
- // create a new batch for this type. The batch number is the number of current batches.
602
- latestBatchNumberForType = _actionBatches . Count ;
603
- _latestBatches [ entityName ] = latestBatchNumberForType ;
604
- // since this entity will now be processed in the latest possible batch,
605
- // we can be assured that it will come after all other associations,
606
- // there's not need to continue checking.
607
- break ;
593
+ return false ;
608
594
}
609
595
}
610
596
}
611
- return latestBatchNumberForType ;
597
+
598
+ return true ;
612
599
}
613
600
614
601
private void AddToBatch ( int batchNumber , EntityInsertAction action )
0 commit comments