@@ -549,29 +549,32 @@ public HbmMapping CompileMappingFor(IEnumerable<System.Type> types)
549
549
throw new ArgumentNullException ( "types" ) ;
550
550
}
551
551
552
- var typeToMap = OrderTypesByHierarchy ( types ) ;
552
+ var entitiesToMap = GetEntitiesToMapOrderedByHierarchy ( types ) ;
553
553
554
554
string defaultAssemblyName = null ;
555
555
string defaultNamespace = null ;
556
- System . Type firstType = typeToMap . FirstOrDefault ( ) ;
557
- if ( firstType != null && typeToMap . All ( t => t . Assembly . Equals ( firstType . Assembly ) ) )
556
+ System . Type firstType = entitiesToMap . FirstOrDefault ( ) ;
557
+ if ( firstType != null && entitiesToMap . All ( t => t . Assembly . Equals ( firstType . Assembly ) ) )
558
558
{
559
559
//NH-2831: always use the full name of the assembly because it may come from GAC
560
560
defaultAssemblyName = firstType . Assembly . GetName ( ) . FullName ;
561
561
}
562
- if ( firstType != null && typeToMap . All ( t => t . Namespace == firstType . Namespace ) )
562
+ if ( firstType != null && entitiesToMap . All ( t => t . Namespace == firstType . Namespace ) )
563
563
{
564
564
defaultNamespace = firstType . Namespace ;
565
565
}
566
566
var mapping = NewHbmMapping ( defaultAssemblyName , defaultNamespace ) ;
567
- foreach ( var type in RootClasses ( typeToMap ) )
567
+ foreach ( var type in RootClasses ( entitiesToMap ) )
568
568
{
569
569
MapRootClass ( type , mapping ) ;
570
570
}
571
- foreach ( var type in Subclasses ( typeToMap ) )
571
+
572
+ var entitiesSet = new HashSet < System . Type > ( entitiesToMap ) ;
573
+ foreach ( var type in Subclasses ( entitiesToMap ) )
572
574
{
573
- AddSubclassMapping ( mapping , type ) ;
575
+ AddSubclassMapping ( mapping , type , entitiesSet ) ;
574
576
}
577
+
575
578
return mapping ;
576
579
}
577
580
@@ -581,21 +584,27 @@ public IEnumerable<HbmMapping> CompileMappingForEach(IEnumerable<System.Type> ty
581
584
{
582
585
throw new ArgumentNullException ( "types" ) ;
583
586
}
584
- var typeToMap = OrderTypesByHierarchy ( types ) ;
587
+
588
+ var entitiesToMap = GetEntitiesToMapOrderedByHierarchy ( types ) ;
585
589
586
590
//NH-2831: always use the full name of the assembly because it may come from GAC
587
- foreach ( var type in RootClasses ( typeToMap ) )
591
+ var mappings = new List < HbmMapping > ( ) ;
592
+ foreach ( var type in RootClasses ( entitiesToMap ) )
588
593
{
589
594
var mapping = NewHbmMapping ( type . Assembly . GetName ( ) . FullName , type . Namespace ) ;
590
595
MapRootClass ( type , mapping ) ;
591
- yield return mapping ;
596
+ mappings . Add ( mapping ) ;
592
597
}
593
- foreach ( var type in Subclasses ( typeToMap ) )
598
+
599
+ var entitiesSet = new HashSet < System . Type > ( entitiesToMap ) ;
600
+ foreach ( var type in Subclasses ( entitiesToMap ) )
594
601
{
595
602
var mapping = NewHbmMapping ( type . Assembly . GetName ( ) . FullName , type . Namespace ) ;
596
- AddSubclassMapping ( mapping , type ) ;
597
- yield return mapping ;
603
+ AddSubclassMapping ( mapping , type , entitiesSet ) ;
604
+ mappings . Add ( mapping ) ;
598
605
}
606
+
607
+ return mappings ;
599
608
}
600
609
601
610
private HbmMapping NewHbmMapping ( string defaultAssemblyName , string defaultNamespace )
@@ -608,40 +617,40 @@ private HbmMapping NewHbmMapping(string defaultAssemblyName, string defaultNames
608
617
return hbmMapping ;
609
618
}
610
619
611
- private IEnumerable < System . Type > Subclasses ( IEnumerable < System . Type > types )
620
+ private IEnumerable < System . Type > Subclasses ( IEnumerable < System . Type > entities )
612
621
{
613
- return types . Where ( type => modelInspector . IsEntity ( type ) && ! modelInspector . IsRootEntity ( type ) ) ;
622
+ return entities . Where ( type => ! modelInspector . IsRootEntity ( type ) ) ;
614
623
}
615
624
616
- private IEnumerable < System . Type > RootClasses ( IEnumerable < System . Type > types )
625
+ private IEnumerable < System . Type > RootClasses ( IEnumerable < System . Type > entities )
617
626
{
618
- return types . Where ( type => modelInspector . IsEntity ( type ) && modelInspector . IsRootEntity ( type ) ) ;
627
+ return entities . Where ( type => modelInspector . IsRootEntity ( type ) ) ;
619
628
}
620
629
621
- private void AddSubclassMapping ( HbmMapping mapping , System . Type type )
630
+ private void AddSubclassMapping ( HbmMapping mapping , System . Type type , ICollection < System . Type > mappedEntities )
622
631
{
623
632
if ( modelInspector . IsTablePerClassHierarchy ( type ) )
624
633
{
625
- MapSubclass ( type , mapping ) ;
634
+ MapSubclass ( type , mapping , mappedEntities ) ;
626
635
}
627
636
else if ( modelInspector . IsTablePerClass ( type ) )
628
637
{
629
- MapJoinedSubclass ( type , mapping ) ;
638
+ MapJoinedSubclass ( type , mapping , mappedEntities ) ;
630
639
}
631
640
else if ( modelInspector . IsTablePerConcreteClass ( type ) )
632
641
{
633
- MapUnionSubclass ( type , mapping ) ;
642
+ MapUnionSubclass ( type , mapping , mappedEntities ) ;
634
643
}
635
644
}
636
645
637
- private void MapUnionSubclass ( System . Type type , HbmMapping mapping )
646
+ private void MapUnionSubclass ( System . Type type , HbmMapping mapping , ICollection < System . Type > mappedEntities )
638
647
{
639
648
var classMapper = new UnionSubclassMapper ( type , mapping ) ;
640
649
641
650
IEnumerable < MemberInfo > candidateProperties = null ;
642
- if ( ! modelInspector . IsEntity ( type . BaseType ) )
651
+ if ( ! mappedEntities . Contains ( type . BaseType ) )
643
652
{
644
- System . Type baseType = GetEntityBaseType ( type ) ;
653
+ var baseType = GetEntityBaseType ( type , mappedEntities ) ;
645
654
if ( baseType != null )
646
655
{
647
656
classMapper . Extends ( baseType ) ;
@@ -659,13 +668,13 @@ private void MapUnionSubclass(System.Type type, HbmMapping mapping)
659
668
MapProperties ( type , propertiesToMap , classMapper ) ;
660
669
}
661
670
662
- private void MapSubclass ( System . Type type , HbmMapping mapping )
671
+ private void MapSubclass ( System . Type type , HbmMapping mapping , ICollection < System . Type > mappedEntities )
663
672
{
664
673
var classMapper = new SubclassMapper ( type , mapping ) ;
665
674
IEnumerable < MemberInfo > candidateProperties = null ;
666
- if ( ! modelInspector . IsEntity ( type . BaseType ) )
675
+ if ( ! mappedEntities . Contains ( type . BaseType ) )
667
676
{
668
- System . Type baseType = GetEntityBaseType ( type ) ;
677
+ var baseType = GetEntityBaseType ( type , mappedEntities ) ;
669
678
if ( baseType != null )
670
679
{
671
680
classMapper . Extends ( baseType ) ;
@@ -701,20 +710,21 @@ private void MapSubclass(System.Type type, HbmMapping mapping)
701
710
InvokeAfterMapSubclass ( type , classMapper ) ;
702
711
}
703
712
704
- private void MapJoinedSubclass ( System . Type type , HbmMapping mapping )
713
+ private void MapJoinedSubclass ( System . Type type , HbmMapping mapping , ICollection < System . Type > mappedEntities )
705
714
{
706
715
var classMapper = new JoinedSubclassMapper ( type , mapping ) ;
707
716
IEnumerable < MemberInfo > candidateProperties = null ;
708
- if ( ! modelInspector . IsEntity ( type . BaseType ) )
717
+ if ( ! mappedEntities . Contains ( type . BaseType ) )
709
718
{
710
- System . Type baseType = GetEntityBaseType ( type ) ;
719
+ var baseType = GetEntityBaseType ( type , mappedEntities ) ;
711
720
if ( baseType != null )
712
721
{
713
722
classMapper . Extends ( baseType ) ;
714
723
classMapper . Key ( km => km . Column ( baseType . Name . ToLowerInvariant ( ) + "_key" ) ) ;
715
724
candidateProperties = membersProvider . GetSubEntityMembers ( type , baseType ) ;
716
725
}
717
726
}
727
+
718
728
candidateProperties = candidateProperties ?? membersProvider . GetSubEntityMembers ( type , type . BaseType ) ;
719
729
IEnumerable < MemberInfo > propertiesToMap =
720
730
candidateProperties . Where ( p => modelInspector . IsPersistentProperty ( p ) && ! modelInspector . IsPersistentId ( p ) ) ;
@@ -726,18 +736,19 @@ private void MapJoinedSubclass(System.Type type, HbmMapping mapping)
726
736
MapProperties ( type , propertiesToMap , classMapper ) ;
727
737
}
728
738
729
- private System . Type GetEntityBaseType ( System . Type type )
739
+ private static System . Type GetEntityBaseType ( System . Type type , ICollection < System . Type > mappedEntities )
730
740
{
731
741
System . Type analyzingType = type ;
732
742
while ( analyzingType != null && analyzingType != typeof ( object ) )
733
743
{
734
744
analyzingType = analyzingType . BaseType ;
735
- if ( modelInspector . IsEntity ( analyzingType ) )
745
+ if ( mappedEntities . Contains ( analyzingType ) )
736
746
{
737
747
return analyzingType ;
738
748
}
739
749
}
740
- return type . GetInterfaces ( ) . FirstOrDefault ( i => modelInspector . IsEntity ( i ) ) ;
750
+
751
+ return type . GetInterfaces ( ) . FirstOrDefault ( i => mappedEntities . Contains ( i ) ) ;
741
752
}
742
753
743
754
private void MapRootClass ( System . Type type , HbmMapping mapping )
@@ -1805,7 +1816,7 @@ public IEnumerable<HbmMapping> CompileMappingForEachExplicitlyAddedEntity()
1805
1816
return CompileMappingForEach ( customizerHolder . GetAllCustomizedEntities ( ) ) ;
1806
1817
}
1807
1818
1808
- private static List < System . Type > OrderTypesByHierarchy ( IEnumerable < System . Type > types )
1819
+ private List < System . Type > GetEntitiesToMapOrderedByHierarchy ( IEnumerable < System . Type > types )
1809
1820
{
1810
1821
var typesCache = new HashSet < System . Type > ( types ) ;
1811
1822
@@ -1815,6 +1826,9 @@ public IEnumerable<HbmMapping> CompileMappingForEachExplicitlyAddedEntity()
1815
1826
var type = typesCache . First ( ) ;
1816
1827
result . AddRange ( type . GetHierarchyFromBase ( ) . Where ( baseType => typesCache . Remove ( baseType ) ) ) ;
1817
1828
}
1829
+
1830
+ result . RemoveAll ( type => ! modelInspector . IsEntity ( type ) ) ;
1831
+
1818
1832
return result ;
1819
1833
}
1820
1834
}
0 commit comments