Skip to content

Commit e6a0b47

Browse files
committed
NH-3807 - Refactoring Mapping.ByCode to reduce use of ReflectedType.
ReflectedType is not in CoreClr. For more information, see: https://github.com/dotnet/coreclr/issues/926 https://web.archive.org/web/20140515064201/http://blogs.msdn.com/b/kingces/archive/2005/08/01/446247.aspx
1 parent be10835 commit e6a0b47

16 files changed

+507
-247
lines changed

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<OutputPath>bin\Debug-2.0\</OutputPath>
4444
<BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath>
4545
<IntermediateOutputPath>obj\Debug-2.0\</IntermediateOutputPath>
46-
<DefineConstants>NET_4_0,FEATURE_SERIALIZATION</DefineConstants>
46+
<DefineConstants>NET_4_0,FEATURE_SERIALIZATION,FEATURE_LEGACY_REFLECTION_API,FEATURE_REFLECTEDTYPE</DefineConstants>
4747
<ErrorReport>prompt</ErrorReport>
4848
<WarningLevel>4</WarningLevel>
4949
<UseVSHostingProcess>false</UseVSHostingProcess>
@@ -57,7 +57,7 @@
5757
<OutputPath>bin\Release-2.0\</OutputPath>
5858
<BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath>
5959
<IntermediateOutputPath>obj\Release-2.0\</IntermediateOutputPath>
60-
<DefineConstants>TRACE;NET_4_0,FEATURE_SERIALIZATION</DefineConstants>
60+
<DefineConstants>TRACE;NET_4_0,FEATURE_SERIALIZATION,FEATURE_LEGACY_REFLECTION_API,FEATURE_REFLECTEDTYPE</DefineConstants>
6161
<ErrorReport>prompt</ErrorReport>
6262
<WarningLevel>4</WarningLevel>
6363
<UseVSHostingProcess>false</UseVSHostingProcess>

src/NHibernate/Mapping/ByCode/AbstractExplicitlyDeclaredModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public void AddAsPropertySplit(SplitDefinition definition)
451451
System.Type propertyContainer = definition.On;
452452
string splitGroupId = definition.GroupId;
453453
MemberInfo member = definition.Member;
454-
var memberKey = member.GetMemberFromDeclaringType();
454+
var memberKey = member.GetMemberFromDeclaringType(propertyContainer);
455455
string splitGroup;
456456
if (!memberSplitGroup.TryGetValue(memberKey, out splitGroup))
457457
{

src/NHibernate/Mapping/ByCode/ConventionModelMapper.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ protected virtual void ComponentParentToFieldAccessor(IModelInspector modelInspe
6161
{
6262
System.Type componentType = member.LocalMember.GetPropertyOrFieldType();
6363
IEnumerable<MemberInfo> persistentProperties =
64-
MembersProvider.GetComponentMembers(componentType).Where(p => ModelInspector.IsPersistentProperty(p));
64+
MembersProvider.GetComponentMembers(componentType).Where(p => ModelInspector.IsPersistentProperty(p, componentType));
6565

66-
MemberInfo parentReferenceProperty = GetComponentParentReferenceProperty(persistentProperties, member.LocalMember.ReflectedType);
66+
MemberInfo parentReferenceProperty = GetComponentParentReferenceProperty(persistentProperties, member.ComponentType, componentType);
6767
if (parentReferenceProperty != null && MatchPropertyToField(parentReferenceProperty))
6868
{
6969
componentMapper.Parent(parentReferenceProperty, cp=> cp.Access(Accessor.Field));
@@ -74,9 +74,9 @@ protected virtual void ComponentParentNoSetterToField(IModelInspector modelInspe
7474
{
7575
System.Type componentType = member.LocalMember.GetPropertyOrFieldType();
7676
IEnumerable<MemberInfo> persistentProperties =
77-
MembersProvider.GetComponentMembers(componentType).Where(p => ModelInspector.IsPersistentProperty(p));
77+
MembersProvider.GetComponentMembers(componentType).Where(p => ModelInspector.IsPersistentProperty(p, componentType));
7878

79-
MemberInfo parentReferenceProperty = GetComponentParentReferenceProperty(persistentProperties, member.LocalMember.ReflectedType);
79+
MemberInfo parentReferenceProperty = GetComponentParentReferenceProperty(persistentProperties, member.ComponentType, componentType);
8080
if (parentReferenceProperty != null && MatchNoSetterProperty(parentReferenceProperty))
8181
{
8282
componentMapper.Parent(parentReferenceProperty, cp => cp.Access(Accessor.NoSetter));
@@ -107,12 +107,12 @@ protected bool MatchReadOnlyProperty(MemberInfo subject)
107107

108108
private bool CanReadCantWriteInsideType(PropertyInfo property)
109109
{
110-
return !property.CanWrite && property.CanRead && property.DeclaringType == property.ReflectedType;
110+
return !property.CanWrite && property.CanRead && property.DeclaringType == property.DeclaringType;
111111
}
112112

113113
private bool CanReadCantWriteInBaseType(PropertyInfo property)
114114
{
115-
if (property.DeclaringType == property.ReflectedType)
115+
if (property.DeclaringType == property.DeclaringType)
116116
{
117117
return false;
118118
}
@@ -155,7 +155,7 @@ protected bool MatchPropertyToField(MemberInfo subject)
155155

156156
protected virtual void NoSetterPoidToField(IModelInspector modelInspector, System.Type type, IClassAttributesMapper classCustomizer)
157157
{
158-
MemberInfo poidPropertyOrField = MembersProvider.GetEntityMembersForPoid(type).FirstOrDefault(modelInspector.IsPersistentId);
158+
MemberInfo poidPropertyOrField = MembersProvider.GetEntityMembersForPoid(type).FirstOrDefault(mi => modelInspector.IsPersistentId(mi, type));
159159
if(MatchNoSetterProperty(poidPropertyOrField))
160160
{
161161
classCustomizer.Id(idm=> idm.Access(Accessor.NoSetter));
@@ -180,7 +180,7 @@ protected bool MatchNoSetterProperty(MemberInfo subject)
180180

181181
protected virtual void NoPoidGuid(IModelInspector modelInspector, System.Type type, IClassAttributesMapper classCustomizer)
182182
{
183-
MemberInfo poidPropertyOrField = MembersProvider.GetEntityMembersForPoid(type).FirstOrDefault(mi => modelInspector.IsPersistentId(mi));
183+
MemberInfo poidPropertyOrField = MembersProvider.GetEntityMembersForPoid(type).FirstOrDefault(mi => modelInspector.IsPersistentId(mi, type));
184184
if (!ReferenceEquals(null, poidPropertyOrField))
185185
{
186186
return;
@@ -313,4 +313,4 @@ public void IsTablePerClassSplit(Func<SplitDefinition, bool, bool> match)
313313
SimpleModelInspector.IsTablePerClassSplit(match);
314314
}
315315
}
316-
}
316+
}

src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public virtual bool IsTablePerConcreteClass(System.Type type)
4646
return IsMappedForTablePerConcreteClassEntities(type);
4747
}
4848

49-
public virtual bool IsOneToOne(MemberInfo member)
49+
public virtual bool IsOneToOne(MemberInfo member, System.Type _)
5050
{
5151
return OneToOneRelations.Contains(member);
5252
}
@@ -56,12 +56,17 @@ public virtual bool IsManyToOne(MemberInfo member)
5656
return ManyToOneRelations.Contains(member);
5757
}
5858

59-
public virtual bool IsManyToManyItem(MemberInfo member)
59+
public virtual bool IsManyToOne(MemberInfo member, System.Type _)
60+
{
61+
return ManyToOneRelations.Contains(member);
62+
}
63+
64+
public virtual bool IsManyToManyItem(MemberInfo member, System.Type _)
6065
{
6166
return ItemManyToManyRelations.Contains(member);
6267
}
6368

64-
public virtual bool IsManyToManyKey(MemberInfo member)
69+
public virtual bool IsManyToManyKey(MemberInfo member, System.Type _)
6570
{
6671
return KeyManyToManyRelations.Contains(member);
6772
}
@@ -71,12 +76,17 @@ public virtual bool IsOneToMany(MemberInfo member)
7176
return OneToManyRelations.Contains(member);
7277
}
7378

74-
public bool IsManyToAny(MemberInfo member)
79+
public virtual bool IsOneToMany(MemberInfo member, System.Type _)
80+
{
81+
return OneToManyRelations.Contains(member);
82+
}
83+
84+
public bool IsManyToAny(MemberInfo member, System.Type _)
7585
{
7686
return ManyToAnyRelations.Contains(member);
7787
}
7888

79-
public virtual bool IsAny(MemberInfo member)
89+
public virtual bool IsAny(MemberInfo member, System.Type _)
8090
{
8191
return Any.Contains(member);
8292
}
@@ -86,6 +96,11 @@ public virtual bool IsPersistentId(MemberInfo member)
8696
return Poids.Contains(member);
8797
}
8898

99+
public virtual bool IsPersistentId(MemberInfo member, System.Type _)
100+
{
101+
return Poids.Contains(member);
102+
}
103+
89104
public bool IsMemberOfComposedId(MemberInfo member)
90105
{
91106
return ComposedIds.Contains(member);
@@ -96,32 +111,57 @@ public virtual bool IsVersion(MemberInfo member)
96111
return VersionProperties.Contains(member);
97112
}
98113

114+
public virtual bool IsVersion(MemberInfo member, System.Type _)
115+
{
116+
return VersionProperties.Contains(member);
117+
}
118+
99119
public virtual bool IsMemberOfNaturalId(MemberInfo member)
100120
{
101121
return NaturalIds.Contains(member);
102122
}
103123

124+
public virtual bool IsMemberOfNaturalId(MemberInfo member, System.Type _)
125+
{
126+
return NaturalIds.Contains(member);
127+
}
128+
104129
public virtual bool IsPersistentProperty(MemberInfo member)
105130
{
106131
return PersistentMembers.Contains(member);
107132
}
108133

134+
public virtual bool IsPersistentProperty(MemberInfo member, System.Type _)
135+
{
136+
return PersistentMembers.Contains(member);
137+
}
138+
109139
public virtual bool IsSet(MemberInfo role)
110140
{
111141
return Sets.Contains(role);
112142
}
113143

144+
public virtual bool IsSet(MemberInfo role, System.Type _)
145+
{
146+
return Sets.Contains(role);
147+
}
148+
114149
public virtual bool IsBag(MemberInfo role)
115150
{
116151
return Bags.Contains(role);
117152
}
118153

119-
public virtual bool IsIdBag(MemberInfo role)
154+
public virtual bool IsBag(MemberInfo role, System.Type _)
155+
{
156+
return Bags.Contains(role);
157+
}
158+
159+
public virtual bool IsIdBag(MemberInfo role, System.Type _)
120160
{
121161
return IdBags.Contains(role);
122162
}
123163

124-
public virtual bool IsList(MemberInfo role)
164+
public virtual bool IsList(MemberInfo role, System.Type _)
125165
{
126166
return Lists.Contains(role);
127167
}
@@ -131,17 +171,27 @@ public virtual bool IsArray(MemberInfo role)
131171
return Arrays.Contains(role);
132172
}
133173

174+
public virtual bool IsArray(MemberInfo role, System.Type _)
175+
{
176+
return Arrays.Contains(role);
177+
}
178+
134179
public virtual bool IsDictionary(MemberInfo role)
135180
{
136181
return Dictionaries.Contains(role);
137182
}
138183

139-
public virtual bool IsProperty(MemberInfo member)
184+
public virtual bool IsDictionary(MemberInfo role, System.Type _)
185+
{
186+
return Dictionaries.Contains(role);
187+
}
188+
189+
public virtual bool IsProperty(MemberInfo member, System.Type _)
140190
{
141191
return Properties.Contains(member);
142192
}
143193

144-
public virtual bool IsDynamicComponent(MemberInfo member)
194+
public virtual bool IsDynamicComponent(MemberInfo member, System.Type _)
145195
{
146196
return DynamicComponents.Contains(member);
147197
}
@@ -153,4 +203,4 @@ public virtual IEnumerable<string> GetPropertiesSplits(System.Type type)
153203

154204
#endregion
155205
}
156-
}
206+
}

src/NHibernate/Mapping/ByCode/IModelInspector.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,41 @@ public interface IModelInspector
1414
bool IsTablePerClassHierarchy(System.Type type);
1515
bool IsTablePerConcreteClass(System.Type type);
1616

17-
bool IsOneToOne(MemberInfo member);
17+
bool IsOneToOne(MemberInfo member, System.Type componentType);
1818
bool IsManyToOne(MemberInfo member);
19-
bool IsManyToManyItem(MemberInfo member);
20-
bool IsManyToManyKey(MemberInfo member);
19+
bool IsManyToOne(MemberInfo member, System.Type componentType);
20+
bool IsManyToManyItem(MemberInfo member, System.Type componentType);
21+
bool IsManyToManyKey(MemberInfo member, System.Type componentType);
2122
bool IsOneToMany(MemberInfo member);
22-
bool IsManyToAny(MemberInfo member);
23+
bool IsOneToMany(MemberInfo member, System.Type componentType);
24+
bool IsManyToAny(MemberInfo member, System.Type componentType);
2325

24-
bool IsAny(MemberInfo member);
26+
bool IsAny(MemberInfo member, System.Type componentType);
2527

2628
bool IsPersistentId(MemberInfo member);
29+
bool IsPersistentId(MemberInfo member, System.Type componentType);
2730
bool IsMemberOfComposedId(MemberInfo member);
2831
bool IsVersion(MemberInfo member);
32+
bool IsVersion(MemberInfo member, System.Type componentType);
2933
bool IsMemberOfNaturalId(MemberInfo member);
34+
bool IsMemberOfNaturalId(MemberInfo member, System.Type componentType);
3035

3136
bool IsPersistentProperty(MemberInfo member);
37+
bool IsPersistentProperty(MemberInfo member, System.Type componentType);
3238

3339
bool IsSet(MemberInfo role);
40+
bool IsSet(MemberInfo role, System.Type componentType);
3441
bool IsBag(MemberInfo role);
35-
bool IsIdBag(MemberInfo role);
36-
bool IsList(MemberInfo role);
42+
bool IsBag(MemberInfo role, System.Type componentType);
43+
bool IsIdBag(MemberInfo role, System.Type componentType);
44+
bool IsList(MemberInfo role, System.Type componentType);
3745
bool IsArray(MemberInfo role);
46+
bool IsArray(MemberInfo role, System.Type componentType);
3847
bool IsDictionary(MemberInfo role);
39-
bool IsProperty(MemberInfo member);
40-
bool IsDynamicComponent(MemberInfo member);
48+
bool IsDictionary(MemberInfo role, System.Type componentType);
49+
bool IsProperty(MemberInfo member, System.Type componentType);
50+
bool IsDynamicComponent(MemberInfo member, System.Type componentType);
4151
System.Type GetDynamicComponentTemplate(MemberInfo member);
4252
IEnumerable<string> GetPropertiesSplits(System.Type type);
4353
}
44-
}
54+
}

src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/ClassCustomizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private void RegisterComponentAsIdMapping<TComponent>(Action<IComponentAsIdMappe
9292
{
9393
foreach (var member in members)
9494
{
95-
var propertyPath = new PropertyPath(PropertyPath, member);
95+
var propertyPath = new PropertyPath(PropertyPath, member, typeof(TEntity));
9696
idMapper(new ComponentAsIdCustomizer<TComponent>(ExplicitDeclarationsHolder, CustomizersHolder, propertyPath));
9797
}
9898
}
@@ -291,4 +291,4 @@ IModelExplicitDeclarationsHolder IConformistHoldersProvider.ExplicitDeclarations
291291
get { return ExplicitDeclarationsHolder; }
292292
}
293293
}
294-
}
294+
}

src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/ComponentElementCustomizer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public void Class<TConcrete>() where TConcrete : TComponent
7171
public void Property<TProperty>(Expression<Func<TComponent, TProperty>> property, Action<IPropertyMapper> mapping)
7272
{
7373
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
74-
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, member), mapping);
74+
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, member, typeof(TComponent)), mapping);
7575
MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property);
76-
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, memberOf), mapping);
76+
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, memberOf, typeof(TComponent)), mapping);
7777
_explicitDeclarationsHolder.AddAsProperty(member);
7878
_explicitDeclarationsHolder.AddAsProperty(memberOf);
7979
}
@@ -87,17 +87,17 @@ public void Component<TNestedComponent>(Expression<Func<TComponent, TNestedCompo
8787
where TNestedComponent : class
8888
{
8989
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
90-
mapping(new ComponentElementCustomizer<TNestedComponent>(_explicitDeclarationsHolder, new PropertyPath(_propertyPath, member), _customizersHolder));
90+
mapping(new ComponentElementCustomizer<TNestedComponent>(_explicitDeclarationsHolder, new PropertyPath(_propertyPath, member, typeof(TComponent)), _customizersHolder));
9191
MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property);
92-
mapping(new ComponentElementCustomizer<TNestedComponent>(_explicitDeclarationsHolder, new PropertyPath(_propertyPath, memberOf), _customizersHolder));
92+
mapping(new ComponentElementCustomizer<TNestedComponent>(_explicitDeclarationsHolder, new PropertyPath(_propertyPath, memberOf, typeof(TComponent)), _customizersHolder));
9393
}
9494

9595
public void ManyToOne<TProperty>(Expression<Func<TComponent, TProperty>> property, Action<IManyToOneMapper> mapping) where TProperty : class
9696
{
9797
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
98-
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, member), mapping);
98+
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, member, typeof(TComponent)), mapping);
9999
MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property);
100-
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, memberOf), mapping);
100+
_customizersHolder.AddCustomizer(new PropertyPath(_propertyPath, memberOf, typeof(TComponent)), mapping);
101101
_explicitDeclarationsHolder.AddAsManyToOneRelation(member);
102102
_explicitDeclarationsHolder.AddAsManyToOneRelation(memberOf);
103103
}
@@ -124,4 +124,4 @@ public void OptimisticLock(bool takeInConsiderationForOptimisticLock)
124124

125125
#endregion
126126
}
127-
}
127+
}

src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/MapKeyComponentCustomizer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public MapKeyComponentCustomizer(IModelExplicitDeclarationsHolder explicitDeclar
2020
public void Property<TProperty>(Expression<Func<TKey, TProperty>> property, Action<IPropertyMapper> mapping)
2121
{
2222
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
23-
customizersHolder.AddCustomizer(new PropertyPath(propertyPath, member), mapping);
23+
customizersHolder.AddCustomizer(new PropertyPath(propertyPath, member, typeof(TKey)), mapping);
2424
}
2525

2626
public void Property<TProperty>(Expression<Func<TKey, TProperty>> property)
@@ -31,9 +31,9 @@ public void Property<TProperty>(Expression<Func<TKey, TProperty>> property)
3131
public void ManyToOne<TProperty>(Expression<Func<TKey, TProperty>> property, Action<IManyToOneMapper> mapping) where TProperty : class
3232
{
3333
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
34-
customizersHolder.AddCustomizer(new PropertyPath(propertyPath, member), mapping);
34+
customizersHolder.AddCustomizer(new PropertyPath(propertyPath, member, typeof(TKey)), mapping);
3535
}
3636

3737
#endregion
3838
}
39-
}
39+
}

0 commit comments

Comments
 (0)