Skip to content

Commit f55795a

Browse files
committed
NH-3214 Improve exception message when trying to perform unsupported non-generic collection mapping in Mapping-By-Code.
1 parent 5e9e273 commit f55795a

File tree

4 files changed

+51
-40
lines changed

4 files changed

+51
-40
lines changed

src/NHibernate/Mapping/ByCode/Impl/AbstractPropertyContainerMapper.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public virtual void OneToOne(MemberInfo property, Action<IOneToOneMapper> mappin
2222
public virtual void Set(MemberInfo property, Action<ISetPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
2323
{
2424
var hbm = new HbmSet {name = property.Name};
25-
System.Type propertyType = property.GetPropertyOrFieldType();
26-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
25+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
2726
collectionMapping(new SetMapper(container, collectionElementType, hbm));
2827
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
2928
AddProperty(hbm);
@@ -32,8 +31,7 @@ public virtual void Set(MemberInfo property, Action<ISetPropertiesMapper> collec
3231
public virtual void Bag(MemberInfo property, Action<IBagPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
3332
{
3433
var hbm = new HbmBag {name = property.Name};
35-
System.Type propertyType = property.GetPropertyOrFieldType();
36-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
34+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
3735
collectionMapping(new BagMapper(container, collectionElementType, hbm));
3836
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
3937
AddProperty(hbm);
@@ -42,8 +40,7 @@ public virtual void Bag(MemberInfo property, Action<IBagPropertiesMapper> collec
4240
public virtual void List(MemberInfo property, Action<IListPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
4341
{
4442
var hbm = new HbmList {name = property.Name};
45-
System.Type propertyType = property.GetPropertyOrFieldType();
46-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
43+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
4744
collectionMapping(new ListMapper(container, collectionElementType, hbm));
4845
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item1 = rel));
4946
AddProperty(hbm);
@@ -66,8 +63,7 @@ public virtual void Map(MemberInfo property, Action<IMapPropertiesMapper> collec
6663
public virtual void IdBag(MemberInfo property, Action<IIdBagPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
6764
{
6865
var hbm = new HbmIdbag { name = property.Name };
69-
System.Type propertyType = property.GetPropertyOrFieldType();
70-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
66+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
7167
collectionMapping(new IdBagMapper(container, collectionElementType, hbm));
7268
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
7369
AddProperty(hbm);

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,28 @@ protected virtual void RegisterIdBagMapping<TElement>(Action<IIdBagPropertiesMap
356356
public void Set<TElement>(string notVisiblePropertyOrFieldName, Action<ISetPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
357357
{
358358
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVisiblePropertyOrFieldName);
359-
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
360-
if(!typeof(TElement).Equals(collectionElementType))
361-
{
362-
throw new MappingException(string.Format("Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}",
363-
notVisiblePropertyOrFieldName, typeof (TEntity).FullName, typeof (TElement).Name, collectionElementType.Name));
364-
}
359+
AssertCollectionElementType<TElement>(notVisiblePropertyOrFieldName, member);
360+
365361
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
366362
RegisterSetMapping<TElement>(collectionMapping, mapping, member, memberOf);
367363
}
368364

365+
366+
private static void AssertCollectionElementType<TElement>(string propertyOrFieldName, MemberInfo memberInfo)
367+
{
368+
System.Type collectionElementType = memberInfo.GetPropertyOrFieldType().DetermineCollectionElementType();
369+
370+
if (typeof (TElement) != collectionElementType)
371+
{
372+
var message = string.Format(
373+
"Wrong collection element type. For the property/field '{0}' of {1} was expected a generic collection of {2} but was {3}",
374+
propertyOrFieldName, typeof (TEntity).FullName, typeof (TElement).Name,
375+
collectionElementType != null ? collectionElementType.Name : "unknown");
376+
throw new MappingException(message);
377+
}
378+
}
379+
380+
369381
public void Set<TElement>(string notVisiblePropertyOrFieldName, Action<ISetPropertiesMapper<TEntity, TElement>> collectionMapping)
370382
{
371383
Set(notVisiblePropertyOrFieldName, collectionMapping, x => { });
@@ -374,12 +386,8 @@ public void Set<TElement>(string notVisiblePropertyOrFieldName, Action<ISetPrope
374386
public void Bag<TElement>(string notVisiblePropertyOrFieldName, Action<IBagPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
375387
{
376388
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVisiblePropertyOrFieldName);
377-
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
378-
if (!typeof(TElement).Equals(collectionElementType))
379-
{
380-
throw new MappingException(string.Format("Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}",
381-
notVisiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TElement).Name, collectionElementType.Name));
382-
}
389+
AssertCollectionElementType<TElement>(notVisiblePropertyOrFieldName, member);
390+
383391
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
384392
RegisterBagMapping<TElement>(collectionMapping, mapping, member, memberOf);
385393
}
@@ -392,12 +400,8 @@ public void Bag<TElement>(string notVisiblePropertyOrFieldName, Action<IBagPrope
392400
public void List<TElement>(string notVisiblePropertyOrFieldName, Action<IListPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
393401
{
394402
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVisiblePropertyOrFieldName);
395-
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
396-
if (!typeof(TElement).Equals(collectionElementType))
397-
{
398-
throw new MappingException(string.Format("Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}",
399-
notVisiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TElement).Name, collectionElementType.Name));
400-
}
403+
AssertCollectionElementType<TElement>(notVisiblePropertyOrFieldName, member);
404+
401405
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
402406
RegisterListMapping<TElement>(collectionMapping, mapping, member, memberOf);
403407
}
@@ -435,12 +439,8 @@ public void Map<TKey, TElement>(string notVisiblePropertyOrFieldName, Action<IMa
435439
public void IdBag<TElement>(string notVisiblePropertyOrFieldName, Action<IIdBagPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
436440
{
437441
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVisiblePropertyOrFieldName);
438-
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
439-
if (!typeof(TElement).Equals(collectionElementType))
440-
{
441-
throw new MappingException(string.Format("Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}",
442-
notVisiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TElement).Name, collectionElementType.Name));
443-
}
442+
AssertCollectionElementType<TElement>(notVisiblePropertyOrFieldName, member);
443+
444444
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
445445
RegisterIdBagMapping<TElement>(collectionMapping, mapping, member, memberOf);
446446
}

src/NHibernate/Mapping/ByCode/Impl/DynamicComponentMapper.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public void OneToOne(MemberInfo property, Action<IOneToOneMapper> mapping)
9292
public void Bag(MemberInfo property, Action<IBagPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
9393
{
9494
var hbm = new HbmBag { name = property.Name };
95-
System.Type propertyType = property.GetPropertyOrFieldType();
96-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
95+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
9796
collectionMapping(new BagMapper(Container, collectionElementType, new NoMemberPropertyMapper(), hbm));
9897
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
9998
AddProperty(hbm);
@@ -102,8 +101,7 @@ public void Bag(MemberInfo property, Action<IBagPropertiesMapper> collectionMapp
102101
public void Set(MemberInfo property, Action<ISetPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
103102
{
104103
var hbm = new HbmSet { name = property.Name };
105-
System.Type propertyType = property.GetPropertyOrFieldType();
106-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
104+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
107105
collectionMapping(new SetMapper(Container, collectionElementType, new NoMemberPropertyMapper(), hbm));
108106
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
109107
AddProperty(hbm);
@@ -112,8 +110,7 @@ public void Set(MemberInfo property, Action<ISetPropertiesMapper> collectionMapp
112110
public void List(MemberInfo property, Action<IListPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
113111
{
114112
var hbm = new HbmList { name = property.Name };
115-
System.Type propertyType = property.GetPropertyOrFieldType();
116-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
113+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
117114
collectionMapping(new ListMapper(Container, collectionElementType, new NoMemberPropertyMapper(), hbm));
118115
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item1 = rel));
119116
AddProperty(hbm);
@@ -135,8 +132,7 @@ public void Map(MemberInfo property, Action<IMapPropertiesMapper> collectionMapp
135132
public void IdBag(MemberInfo property, Action<IIdBagPropertiesMapper> collectionMapping, Action<ICollectionElementRelation> mapping)
136133
{
137134
var hbm = new HbmIdbag { name = property.Name };
138-
System.Type propertyType = property.GetPropertyOrFieldType();
139-
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
135+
System.Type collectionElementType = property.DetermineRequiredCollectionElementType();
140136
collectionMapping(new IdBagMapper(Container, collectionElementType, new NoMemberPropertyMapper(), hbm));
141137
mapping(new CollectionElementRelation(collectionElementType, MapDoc, rel => hbm.Item = rel));
142138
AddProperty(hbm);

src/NHibernate/Mapping/ByCode/TypeExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,25 @@ public static System.Type DetermineCollectionElementType(this System.Type generi
203203
return null;
204204
}
205205

206+
207+
public static System.Type DetermineRequiredCollectionElementType(this MemberInfo collectionProperty)
208+
{
209+
System.Type propertyType = collectionProperty.GetPropertyOrFieldType();
210+
System.Type collectionElementType = propertyType.DetermineCollectionElementType();
211+
212+
if (collectionElementType == null)
213+
{
214+
var message = string.Format(
215+
"Unable to determine collection element type for the property/field '{0}' of {1}. The collection must be generic.",
216+
collectionProperty.Name,
217+
collectionProperty.DeclaringType != null ? collectionProperty.DeclaringType.FullName : "<global>");
218+
throw new MappingException(message);
219+
}
220+
221+
return collectionElementType;
222+
}
223+
224+
206225
public static System.Type DetermineCollectionElementOrDictionaryValueType(this System.Type genericCollection)
207226
{
208227
if (genericCollection.IsGenericType)

0 commit comments

Comments
 (0)