Skip to content

Commit 1155c65

Browse files
committed
Remove unnecessary type lookup
1 parent db22eaa commit 1155c65

File tree

10 files changed

+99
-27
lines changed

10 files changed

+99
-27
lines changed

src/NHibernate/Cfg/XmlHbmBinding/ClassCompositeIdBinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private Property CreateProperty(ToOne value, string propertyName, System.Type pa
190190
{
191191
if (parentClass != null && value.IsSimpleValue)
192192
{
193-
value.SetTypeUsingReflection(parentClass.AssemblyQualifiedName, propertyName,
193+
value.SetTypeUsingReflection(parentClass, propertyName,
194194
keyManyToOneSchema.access ?? mappings.DefaultAccess);
195195
}
196196

@@ -242,7 +242,7 @@ private Property CreateProperty(SimpleValue value, string propertyName, System.T
242242
{
243243
if (parentClass != null && value.IsSimpleValue)
244244
{
245-
value.SetTypeUsingReflection(parentClass.AssemblyQualifiedName, propertyName,
245+
value.SetTypeUsingReflection(parentClass, propertyName,
246246
keyPropertySchema.access ?? mappings.DefaultAccess);
247247
}
248248

src/NHibernate/Cfg/XmlHbmBinding/ClassIdBinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private void CreateIdentifierProperty(HbmId idSchema, PersistentClass rootClass,
4545
if (idSchema.name != null)
4646
{
4747
string access = idSchema.access ?? mappings.DefaultAccess;
48-
id.SetTypeUsingReflection(rootClass.MappedClass == null ? null : rootClass.MappedClass.AssemblyQualifiedName, idSchema.name, access);
48+
id.SetTypeUsingReflection(rootClass.MappedClass, idSchema.name, access);
4949

5050
var property = new Property(id) { Name = idSchema.name };
5151

@@ -85,4 +85,4 @@ private static void BindUnsavedValue(HbmId idSchema, SimpleValue id)
8585
id.NullValue = idSchema.unsavedvalue ?? (id.IdentifierGeneratorStrategy == "assigned" ? "undefined" : null);
8686
}
8787
}
88-
}
88+
}

src/NHibernate/Cfg/XmlHbmBinding/PropertiesBinder.cs

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void Bind(IEnumerable<IEntityPropertyMapping> properties, Table table, ID
103103
{
104104
var value = new SimpleValue(table);
105105
new ValuePropertyBinder(value, Mappings).BindSimpleValue(propertyMapping, propertyName, true);
106-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
106+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
107107
BindValueProperty(propertyMapping, property);
108108
}
109109
else if ((collectionMapping = entityPropertyMapping as ICollectionPropertiesMapping) != null)
@@ -124,14 +124,14 @@ public void Bind(IEnumerable<IEntityPropertyMapping> properties, Table table, ID
124124
var subpath = propertyName == null ? null : StringHelper.Qualify(propertyBasePath, propertyName);
125125
var value = CreateNewComponent(table);
126126
BindComponent(propertiesMapping, value, null, entityName, subpath, componetDefaultNullable, inheritedMetas);
127-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
127+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
128128
BindComponentProperty(propertiesMapping, property, value);
129129
}
130130
else if ((manyToOneMapping = entityPropertyMapping as HbmManyToOne) != null)
131131
{
132132
var value = new ManyToOne(table);
133133
BindManyToOne(manyToOneMapping, value, propertyName, true);
134-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
134+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
135135
BindManyToOneProperty(manyToOneMapping, property);
136136
}
137137
else if ((componentMapping = entityPropertyMapping as HbmComponent) != null)
@@ -141,14 +141,14 @@ public void Bind(IEnumerable<IEntityPropertyMapping> properties, Table table, ID
141141
// NH: Modified from H2.1 to allow specifying the type explicitly using class attribute
142142
System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(componentMapping.Class, mappedClass, propertyName, componentMapping.Access);
143143
BindComponent(componentMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas);
144-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
144+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
145145
BindComponentProperty(componentMapping, property, value);
146146
}
147147
else if ((oneToOneMapping = entityPropertyMapping as HbmOneToOne) != null)
148148
{
149149
var value = new OneToOne(table, persistentClass);
150150
BindOneToOne(oneToOneMapping, value);
151-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
151+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
152152
BindOneToOneProperty(oneToOneMapping, property);
153153
}
154154
else if ((dynamicComponentMapping = entityPropertyMapping as HbmDynamicComponent) != null)
@@ -158,14 +158,14 @@ public void Bind(IEnumerable<IEntityPropertyMapping> properties, Table table, ID
158158
// NH: Modified from H2.1 to allow specifying the type explicitly using class attribute
159159
System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(dynamicComponentMapping.Class, mappedClass, propertyName, dynamicComponentMapping.Access);
160160
BindComponent(dynamicComponentMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas);
161-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
161+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
162162
BindComponentProperty(dynamicComponentMapping, property, value);
163163
}
164164
else if ((anyMapping = entityPropertyMapping as HbmAny) != null)
165165
{
166166
var value = new Any(table);
167167
BindAny(anyMapping, value, true);
168-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
168+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
169169
BindAnyProperty(anyMapping, property);
170170
}
171171
else if ((nestedCompositeElementMapping = entityPropertyMapping as HbmNestedCompositeElement) != null)
@@ -179,19 +179,19 @@ public void Bind(IEnumerable<IEntityPropertyMapping> properties, Table table, ID
179179
// NH: Modified from H2.1 to allow specifying the type explicitly using class attribute
180180
System.Type reflectedClass = mappedClass == null ? null : GetPropertyType(nestedCompositeElementMapping.Class, mappedClass, propertyName, nestedCompositeElementMapping.access);
181181
BindComponent(nestedCompositeElementMapping, value, reflectedClass, entityName, subpath, componetDefaultNullable, inheritedMetas);
182-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
182+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
183183
}
184184
else if ((keyPropertyMapping = entityPropertyMapping as HbmKeyProperty) != null)
185185
{
186186
var value = new SimpleValue(table);
187187
new ValuePropertyBinder(value, Mappings).BindSimpleValue(keyPropertyMapping, propertyName, componetDefaultNullable);
188-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
188+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
189189
}
190190
else if ((keyManyToOneMapping = entityPropertyMapping as HbmKeyManyToOne) != null)
191191
{
192192
var value = new ManyToOne(table);
193193
BindKeyManyToOne(keyManyToOneMapping, value, propertyName, componetDefaultNullable);
194-
property = CreateProperty(entityPropertyMapping, className, value, inheritedMetas);
194+
property = CreateProperty(entityPropertyMapping, mappedClass, value, inheritedMetas);
195195
}
196196

197197
if (property != null)
@@ -402,31 +402,52 @@ private void BindCollectionProperty(ICollectionPropertiesMapping collectionMappi
402402
property.Cascade = collectionMapping.Cascade ?? mappings.DefaultCascade;
403403
}
404404

405-
private Property CreateProperty(IEntityPropertyMapping propertyMapping, string propertyOwnerClassName, IValue value, IDictionary<string, MetaAttribute> inheritedMetas)
405+
private Property CreateProperty(IEntityPropertyMapping propertyMapping, System.Type propertyOwnerType, SimpleValue value, IDictionary<string, MetaAttribute> inheritedMetas)
406406
{
407407
if (string.IsNullOrEmpty(propertyMapping.Name))
408408
{
409-
throw new MappingException("A property mapping must define the name attribute [" + propertyOwnerClassName + "]");
409+
throw new MappingException("A property mapping must define the name attribute [" + propertyOwnerType + "]");
410410
}
411411

412412
var propertyAccessorName = GetPropertyAccessorName(propertyMapping.Access);
413413

414-
if (!string.IsNullOrEmpty(propertyOwnerClassName) && value.IsSimpleValue)
415-
value.SetTypeUsingReflection(propertyOwnerClassName, propertyMapping.Name, propertyAccessorName);
414+
if (propertyOwnerType != null && value.IsSimpleValue)
415+
value.SetTypeUsingReflection(propertyOwnerType, propertyMapping.Name, propertyAccessorName);
416416

417417
var property = new Property
418-
{
419-
Name = propertyMapping.Name,
420-
PropertyAccessorName = propertyAccessorName,
421-
Value = value,
422-
IsLazy = propertyMapping.IsLazyProperty,
423-
LazyGroup = propertyMapping.GetLazyGroup(),
424-
IsOptimisticLocked = propertyMapping.OptimisticLock,
425-
MetaAttributes = GetMetas(propertyMapping, inheritedMetas)
426-
};
418+
{
419+
Name = propertyMapping.Name,
420+
PropertyAccessorName = propertyAccessorName,
421+
Value = value,
422+
IsLazy = propertyMapping.IsLazyProperty,
423+
LazyGroup = propertyMapping.GetLazyGroup(),
424+
IsOptimisticLocked = propertyMapping.OptimisticLock,
425+
MetaAttributes = GetMetas(propertyMapping, inheritedMetas)
426+
};
427427

428428
return property;
429429
}
430+
431+
private Property CreateProperty(IEntityPropertyMapping propertyMapping, string propertyOwnerClassName, Mapping.Collection value, IDictionary<string, MetaAttribute> inheritedMetas)
432+
{
433+
if (string.IsNullOrEmpty(propertyMapping.Name))
434+
{
435+
throw new MappingException("A property mapping must define the name attribute [" + propertyOwnerClassName + "]");
436+
}
437+
438+
var propertyAccessorName = GetPropertyAccessorName(propertyMapping.Access);
439+
440+
return new Property
441+
{
442+
Name = propertyMapping.Name,
443+
PropertyAccessorName = propertyAccessorName,
444+
Value = value,
445+
IsLazy = propertyMapping.IsLazyProperty,
446+
LazyGroup = propertyMapping.GetLazyGroup(),
447+
IsOptimisticLocked = propertyMapping.OptimisticLock,
448+
MetaAttributes = GetMetas(propertyMapping, inheritedMetas)
449+
};
450+
}
430451

431452
private string GetPropertyAccessorName(string propertyMappedAccessor)
432453
{

src/NHibernate/Mapping/Any.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ public void ResetCachedType()
7272
_type = GetLazyType();
7373
}
7474

75+
// Since v5.6
76+
[Obsolete("This method is not used and will be removed in a future version")]
7577
public override void SetTypeUsingReflection(string className, string propertyName, string access)
7678
{
7779
}
7880

81+
public override void SetTypeUsingReflection(System.Type propertyOwnerType, string propertyName, string access)
82+
{
83+
}
84+
7985
/// <summary>
8086
/// Get or set the metatype
8187
/// </summary>

src/NHibernate/Mapping/Collection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ public virtual bool IsAlternateUniqueKey
593593
get { return false; }
594594
}
595595

596+
// Since v5.6
597+
[Obsolete("This method is not used and will be removed in a future version")]
596598
public void SetTypeUsingReflection(string className, string propertyName, string access)
597599
{
598600
}

src/NHibernate/Mapping/Component.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,16 @@ public Component(Component component)
105105
owner = component.Owner;
106106
}
107107

108+
// Since v5.6
109+
[Obsolete("This method is not used and will be removed in a future version")]
108110
public override void SetTypeUsingReflection(string className, string propertyName, string accesorName)
109111
{
110112
}
111113

114+
public override void SetTypeUsingReflection(System.Type propertyOwnerType, string propertyName, string accesorName)
115+
{
116+
}
117+
112118
/// <summary></summary>
113119
public bool IsEmbedded
114120
{

src/NHibernate/Mapping/IValue.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using NHibernate.Engine;
34
using NHibernate.Type;
@@ -78,6 +79,8 @@ public interface IValue
7879

7980
FetchMode FetchMode { get; }
8081

82+
// Since v5.6
83+
[Obsolete("This method is not used and will be removed in a future version")]
8184
void SetTypeUsingReflection(string className, string propertyName, string accesorName);
8285

8386
object Accept(IValueVisitor visitor);

src/NHibernate/Mapping/OneToMany.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public bool IsAlternateUniqueKey
125125
get { return false; }
126126
}
127127

128+
// Since v5.6
129+
[Obsolete("This method is not used and will be removed in a future version")]
128130
public void SetTypeUsingReflection(string className, string propertyName, string accesorName)
129131
{
130132
}

src/NHibernate/Mapping/SimpleValue.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ public bool IsAlternateUniqueKey
341341
set { isAlternateUniqueKey = value; }
342342
}
343343

344+
// Since v5.6
345+
[Obsolete("This method is not used and will be removed in a future version")]
344346
public virtual void SetTypeUsingReflection(string className, string propertyName, string accesorName)
345347
{
346348
if (typeName == null)
@@ -359,6 +361,25 @@ public virtual void SetTypeUsingReflection(string className, string propertyName
359361
}
360362
}
361363
}
364+
365+
public virtual void SetTypeUsingReflection(System.Type propertyOwnerType, string propertyName, string accessorName)
366+
{
367+
if (typeName == null)
368+
{
369+
if (propertyOwnerType == null)
370+
{
371+
throw new MappingException("you must specify types for a dynamic entity: " + propertyName);
372+
}
373+
try
374+
{
375+
typeName = ReflectHelper.ReflectedPropertyClass(propertyOwnerType, propertyName, accessorName).AssemblyQualifiedName;
376+
}
377+
catch (HibernateException he)
378+
{
379+
throw new MappingException("Problem trying to set property type by reflection", he);
380+
}
381+
}
382+
}
362383

363384
public virtual object Accept(IValueVisitor visitor)
364385
{

src/NHibernate/Mapping/ToOne.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public bool IsLazy
5454
/// </summary>
5555
public abstract override void CreateForeignKey();
5656

57+
// Since v5.6
58+
[Obsolete("This method is not used and will be removed in a future version")]
5759
public override void SetTypeUsingReflection(string className, string propertyName, string accesorName)
5860
{
5961
if (referencedEntityName == null)
@@ -63,6 +65,15 @@ public override void SetTypeUsingReflection(string className, string propertyNam
6365
}
6466
}
6567

68+
public override void SetTypeUsingReflection(System.Type propertyOwnerType, string propertyName, string accessorName)
69+
{
70+
if (referencedEntityName == null)
71+
{
72+
System.Type refType = ReflectHelper.ReflectedPropertyClass(propertyOwnerType, propertyName, accessorName);
73+
referencedEntityName = refType.FullName;
74+
}
75+
}
76+
6677
public override bool IsValid(Engine.IMapping mapping)
6778
{
6879
if (referencedEntityName == null)

0 commit comments

Comments
 (0)