6
6
7
7
namespace FluentNHibernate . Visitors
8
8
{
9
- public delegate void PairBiDirectionalManyToManySidesDelegate ( MappingModel . Collections . CollectionMapping current , IEnumerable < MappingModel . Collections . CollectionMapping > possibles , bool wasResolved ) ;
9
+ public delegate void PairBiDirectionalManyToManySidesDelegate ( CollectionMapping current , IEnumerable < CollectionMapping > possibles , bool wasResolved ) ;
10
10
11
11
public class RelationshipPairingVisitor : DefaultMappingModelVisitor
12
12
{
13
13
readonly PairBiDirectionalManyToManySidesDelegate userControlledPair ;
14
- readonly List < MappingModel . Collections . CollectionMapping > manyToManys = new List < MappingModel . Collections . CollectionMapping > ( ) ;
15
- readonly List < MappingModel . Collections . CollectionMapping > oneToManys = new List < MappingModel . Collections . CollectionMapping > ( ) ;
14
+ readonly List < CollectionMapping > manyToManys = new List < CollectionMapping > ( ) ;
15
+ readonly List < CollectionMapping > oneToManys = new List < CollectionMapping > ( ) ;
16
16
readonly List < ManyToOneMapping > references = new List < ManyToOneMapping > ( ) ;
17
17
18
18
public RelationshipPairingVisitor ( PairBiDirectionalManyToManySidesDelegate userControlledPair )
@@ -41,7 +41,7 @@ public override void ProcessManyToOne(ManyToOneMapping manyToOneMapping)
41
41
references . Add ( manyToOneMapping ) ;
42
42
}
43
43
44
- static void PairOneToManys ( IEnumerable < MappingModel . Collections . CollectionMapping > collections , IEnumerable < ManyToOneMapping > refs )
44
+ static void PairOneToManys ( IEnumerable < CollectionMapping > collections , IEnumerable < ManyToOneMapping > refs )
45
45
{
46
46
var orderedCollections = collections . OrderBy ( x => x . Name ) . ToArray ( ) ;
47
47
var orderedRefs = refs . OrderBy ( x => x . Name ) . ToArray ( ) ;
@@ -58,7 +58,7 @@ static void PairOneToManys(IEnumerable<MappingModel.Collections.CollectionMappin
58
58
}
59
59
}
60
60
61
- void PairManyToManys ( IEnumerable < MappingModel . Collections . CollectionMapping > rs )
61
+ void PairManyToManys ( IEnumerable < CollectionMapping > rs )
62
62
{
63
63
if ( ! rs . Any ( ) ) return ;
64
64
@@ -72,9 +72,10 @@ void PairManyToManys(IEnumerable<MappingModel.Collections.CollectionMapping> rs)
72
72
// try to pair the current side with the potential other sides
73
73
var mapping = current ;
74
74
75
- if ( candidates . Count ( ) == 1 )
75
+ var candidatesCount = candidates . Count ( ) ;
76
+ if ( candidatesCount == 1 )
76
77
mapping = PairExactMatches ( rs , current , candidates ) ;
77
- else if ( candidates . Count ( ) > 1 )
78
+ else if ( candidatesCount > 1 )
78
79
mapping = PairFuzzyMatches ( rs , current , candidates ) ;
79
80
80
81
if ( mapping == null )
@@ -89,7 +90,7 @@ void PairManyToManys(IEnumerable<MappingModel.Collections.CollectionMapping> rs)
89
90
90
91
// both collections have been paired, so remove them
91
92
// from the available collections
92
- PairManyToManys ( rs . Except ( mapping , ( MappingModel . Collections . CollectionMapping ) mapping . OtherSide ) ) ;
93
+ PairManyToManys ( rs . Except ( mapping , ( CollectionMapping ) mapping . OtherSide ) ) ;
93
94
}
94
95
95
96
static string GetMemberName ( Member member )
@@ -100,7 +101,7 @@ static string GetMemberName(Member member)
100
101
return member . Name ;
101
102
}
102
103
103
- static LikenessContainer GetLikeness ( MappingModel . Collections . CollectionMapping currentMapping , MappingModel . Collections . CollectionMapping mapping )
104
+ static LikenessContainer GetLikeness ( CollectionMapping currentMapping , CollectionMapping mapping )
104
105
{
105
106
var currentMemberName = GetMemberName ( currentMapping . Member ) ;
106
107
var mappingMemberName = GetMemberName ( mapping . Member ) ;
@@ -114,18 +115,23 @@ static LikenessContainer GetLikeness(MappingModel.Collections.CollectionMapping
114
115
} ;
115
116
}
116
117
117
- static bool both_collections_point_to_each_others_types ( MappingModel . Collections . CollectionMapping left , MappingModel . Collections . CollectionMapping right )
118
+ static bool both_collections_point_to_each_others_types ( CollectionMapping left , CollectionMapping right )
118
119
{
119
120
return left . ContainingEntityType == right . ChildType &&
120
121
left . ChildType == right . ContainingEntityType ;
121
122
}
122
123
124
+ static bool self_referenced_relation_does_not_point_to_itself ( CollectionMapping left , CollectionMapping right )
125
+ {
126
+ return left . ChildType == left . ContainingEntityType && right . ChildType == right . ContainingEntityType && left != right ;
127
+ }
128
+
123
129
static bool likeness_within_threshold ( LikenessContainer current )
124
130
{
125
131
return current . Differences != current . CurrentMemberName . Length ;
126
132
}
127
133
128
- static MappingModel . Collections . CollectionMapping PairFuzzyMatches ( IEnumerable < MappingModel . Collections . CollectionMapping > rs , MappingModel . Collections . CollectionMapping current , IEnumerable < MappingModel . Collections . CollectionMapping > potentialOtherSides )
134
+ static CollectionMapping PairFuzzyMatches ( IEnumerable < CollectionMapping > rs , CollectionMapping current , IEnumerable < CollectionMapping > potentialOtherSides )
129
135
{
130
136
// no exact matches found, drop down to a levenshtein distance
131
137
var mapping = current ;
@@ -157,7 +163,7 @@ static MappingModel.Collections.CollectionMapping PairFuzzyMatches(IEnumerable<M
157
163
return mapping ;
158
164
}
159
165
160
- static MappingModel . Collections . CollectionMapping PairExactMatches ( IEnumerable < MappingModel . Collections . CollectionMapping > rs , MappingModel . Collections . CollectionMapping current , IEnumerable < MappingModel . Collections . CollectionMapping > potentialOtherSides )
166
+ static CollectionMapping PairExactMatches ( IEnumerable < CollectionMapping > rs , CollectionMapping current , IEnumerable < CollectionMapping > potentialOtherSides )
161
167
{
162
168
var otherSide = potentialOtherSides . Single ( ) ;
163
169
@@ -172,9 +178,10 @@ static MappingModel.Collections.CollectionMapping PairExactMatches(IEnumerable<M
172
178
return mapping ;
173
179
}
174
180
175
- static MappingModel . Collections . CollectionMapping FindAlternative ( IEnumerable < MappingModel . Collections . CollectionMapping > rs , MappingModel . Collections . CollectionMapping current , MappingModel . Collections . CollectionMapping otherSide )
181
+ static CollectionMapping FindAlternative ( IEnumerable < CollectionMapping > rs , CollectionMapping current , CollectionMapping otherSide )
176
182
{
177
183
var alternative = rs
184
+ . Where ( x => self_referenced_relation_does_not_point_to_itself ( x , current ) )
178
185
. Where ( x => x . ContainingEntityType == current . ContainingEntityType )
179
186
. Select ( x => GetLikeness ( x , otherSide ) )
180
187
. OrderBy ( x => x . Differences )
@@ -195,7 +202,7 @@ static bool AnyHaveSameLikeness(IEnumerable<LikenessContainer> likenesses, Liken
195
202
196
203
class LikenessContainer
197
204
{
198
- public MappingModel . Collections . CollectionMapping Collection { get ; set ; }
205
+ public CollectionMapping Collection { get ; set ; }
199
206
public string CurrentMemberName { get ; set ; }
200
207
public string MappingMemberName { get ; set ; }
201
208
public int Differences { get ; set ; }
0 commit comments