Skip to content

Commit 2a7cd33

Browse files
committed
not visible checking declared types
SVN: trunk@5792
1 parent 9ed5dfd commit 2a7cd33

File tree

5 files changed

+221
-18
lines changed

5 files changed

+221
-18
lines changed

src/NHibernate.Test/MappingByCode/ExpliticMappingTests/AllPropertiesRegistrationTests.cs

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ public void WhenMapPropertiesInTheBaseJumpedClassThenMapInInherited()
154154
// ignoring MyClass and using Inherited, as root-class, I will try to map all properties using the base class.
155155
// NH have to recognize the case and map those properties in the inherited.
156156
var inspector = new SimpleModelInspector();
157+
inspector.IsEntity((type, declared) => type == typeof(Inherited));
157158
inspector.IsRootEntity((type, declared) => type == typeof(Inherited));
158-
var mapper = new ModelMapper();
159+
var mapper = new ModelMapper(inspector);
159160
mapper.Class<MyClass>(mc =>
160161
{
161162
mc.Id(x => x.Id);
@@ -225,8 +226,9 @@ public void WhenMapPropertiesInTheBaseJumpedClassUsingMemberNameThenMapInInherit
225226
// ignoring MyClass and using Inherited, as root-class, I will try to map all properties using the base class.
226227
// NH have to recognize the case and map those properties in the inherited.
227228
var inspector = new SimpleModelInspector();
228-
inspector.IsRootEntity((type, declared) => type == typeof (Inherited));
229-
var mapper = new ModelMapper();
229+
inspector.IsEntity((type, declared) => type == typeof(Inherited));
230+
inspector.IsRootEntity((type, declared) => type == typeof(Inherited));
231+
var mapper = new ModelMapper(inspector);
230232
mapper.Class<MyClass>(mc =>
231233
{
232234
mc.Id(x => x.Id);
@@ -256,5 +258,126 @@ public void WhenMapPropertiesInTheBaseJumpedClassUsingMemberNameThenMapInInherit
256258
"DynamicCompo");
257259
hbmClass.Properties.Select(p => p.Access).All(x => x.Satisfy(access => access.Contains("field.")));
258260
}
261+
262+
[Test]
263+
public void WhenMapBagWithWrongElementTypeThenThrows()
264+
{
265+
var mapper = new ModelMapper();
266+
Executing.This(() =>
267+
mapper.Class<MyClass>(mc =>
268+
{
269+
mc.Id(x => x.Id);
270+
mc.Bag<int>("Bag", y => y.Access(Accessor.Field));
271+
})).Should().Throw<MappingException>();
272+
}
273+
274+
[Test]
275+
public void WhenMapIdBagWithWrongElementTypeThenThrows()
276+
{
277+
var mapper = new ModelMapper();
278+
Executing.This(() =>
279+
mapper.Class<MyClass>(mc =>
280+
{
281+
mc.Id(x => x.Id);
282+
mc.IdBag<int>("IdBag", y => y.Access(Accessor.Field));
283+
})).Should().Throw<MappingException>();
284+
}
285+
286+
[Test]
287+
public void WhenMapSetWithWrongElementTypeThenThrows()
288+
{
289+
var mapper = new ModelMapper();
290+
Executing.This(() =>
291+
mapper.Class<MyClass>(mc =>
292+
{
293+
mc.Id(x => x.Id);
294+
mc.Set<int>("Set", y => y.Access(Accessor.Field));
295+
})).Should().Throw<MappingException>();
296+
}
297+
298+
[Test]
299+
public void WhenMapListWithWrongElementTypeThenThrows()
300+
{
301+
var mapper = new ModelMapper();
302+
Executing.This(() =>
303+
mapper.Class<MyClass>(mc =>
304+
{
305+
mc.Id(x => x.Id);
306+
mc.Set<int>("Set", y => y.Access(Accessor.Field));
307+
})).Should().Throw<MappingException>();
308+
}
309+
310+
[Test]
311+
public void WhenMapDictionaryWithWrongKeyTypeThenThrows()
312+
{
313+
var mapper = new ModelMapper();
314+
Executing.This(() =>
315+
mapper.Class<MyClass>(mc =>
316+
{
317+
mc.Id(x => x.Id);
318+
mc.Map<string, string>("Map", y => y.Access(Accessor.Field));
319+
})).Should().Throw<MappingException>();
320+
}
321+
322+
[Test]
323+
public void WhenMapDictionaryWithWrongValueTypeThenThrows()
324+
{
325+
var mapper = new ModelMapper();
326+
Executing.This(() =>
327+
mapper.Class<MyClass>(mc =>
328+
{
329+
mc.Id(x => x.Id);
330+
mc.Map<int, int>("Map", y => y.Access(Accessor.Field));
331+
})).Should().Throw<MappingException>();
332+
}
333+
334+
[Test]
335+
public void WhenMapComponentWithWrongElementTypeThenThrows()
336+
{
337+
var mapper = new ModelMapper();
338+
Executing.This(() =>
339+
mapper.Class<MyClass>(mc =>
340+
{
341+
mc.Id(x => x.Id);
342+
mc.Component<object>("Compo", y => y.Access(Accessor.Field));
343+
})).Should().Throw<MappingException>();
344+
}
345+
346+
347+
[Test]
348+
public void WhenMapOneToOneWithWrongTypeThenThrows()
349+
{
350+
var mapper = new ModelMapper();
351+
Executing.This(() =>
352+
mapper.Class<MyClass>(mc =>
353+
{
354+
mc.Id(x => x.Id);
355+
mc.OneToOne<object>("OneToOne", y => y.Access(Accessor.Field));
356+
})).Should().Throw<MappingException>();
357+
}
358+
359+
[Test]
360+
public void WhenMapManyToOneWithWrongTypeThenThrows()
361+
{
362+
var mapper = new ModelMapper();
363+
Executing.This(() =>
364+
mapper.Class<MyClass>(mc =>
365+
{
366+
mc.Id(x => x.Id);
367+
mc.ManyToOne<object>("ManyToOne", y => y.Access(Accessor.Field));
368+
})).Should().Throw<MappingException>();
369+
}
370+
371+
[Test]
372+
public void WhenMapAnyWithWrongTypeThenThrows()
373+
{
374+
var mapper = new ModelMapper();
375+
Executing.This(() =>
376+
mapper.Class<MyClass>(mc =>
377+
{
378+
mc.Id(x => x.Id);
379+
mc.Any<Related>("Any", typeof(int),y => y.Access(Accessor.Field));
380+
})).Should().Throw<MappingException>();
381+
}
259382
}
260383
}

src/NHibernate.Test/MappingByCode/ExpliticMappingTests/MappingOfPrivateMembersOnRootEntity.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,16 @@ public void MapClassWithIdAndProperty()
4848
hbmProperty.access.Should().Be("field");
4949
hbmProperty.length.Should().Be("150");
5050
}
51+
52+
[Test]
53+
public void WhenPrivateMemberDoesNotExistsThenThrow()
54+
{
55+
var mapper = new ModelMapper();
56+
Executing.This(() =>
57+
mapper.Class<MyClass>(ca =>
58+
{
59+
ca.Property("pizza", map => map.Length(150));
60+
})).Should().Throw<MappingException>();
61+
}
5162
}
5263
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public ClassCustomizer(IModelExplicitDeclarationsHolder explicitDeclarationsHold
1818
throw new ArgumentNullException("explicitDeclarationsHolder");
1919
}
2020
explicitDeclarationsHolder.AddAsRootEntity(typeof (TEntity));
21+
22+
// Add an empty customizer as a way to register the class as explicity declared
23+
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => { });
2124
}
2225

2326
private Dictionary<string, IJoinMapper<TEntity>> JoinCustomizers

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

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected virtual void RegisterNoVisiblePropertyMapping(string notVidiblePropert
5555
{
5656
// even seems repetitive, before unify this registration with the registration using Expression take in account that reflection operations
5757
// done unsing expressions are faster than those done with pure reflection.
58-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
58+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
5959
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
6060
RegistePropertyMapping(mapping, member, memberOf);
6161
}
@@ -346,7 +346,13 @@ protected virtual void RegisterIdBagMapping<TElement>(Action<IIdBagPropertiesMap
346346

347347
public void Set<TElement>(string notVidiblePropertyOrFieldName, Action<ISetPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
348348
{
349-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
349+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
350+
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
351+
if(!typeof(TElement).Equals(collectionElementType))
352+
{
353+
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}",
354+
notVidiblePropertyOrFieldName, typeof (TEntity).FullName, typeof (TElement).Name, collectionElementType.Name));
355+
}
350356
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
351357
RegisterSetMapping<TElement>(collectionMapping, mapping, member, memberOf);
352358
}
@@ -358,7 +364,13 @@ public void Set<TElement>(string notVidiblePropertyOrFieldName, Action<ISetPrope
358364

359365
public void Bag<TElement>(string notVidiblePropertyOrFieldName, Action<IBagPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
360366
{
361-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
367+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
368+
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
369+
if (!typeof(TElement).Equals(collectionElementType))
370+
{
371+
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}",
372+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TElement).Name, collectionElementType.Name));
373+
}
362374
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
363375
RegisterBagMapping<TElement>(collectionMapping, mapping, member, memberOf);
364376
}
@@ -370,7 +382,13 @@ public void Bag<TElement>(string notVidiblePropertyOrFieldName, Action<IBagPrope
370382

371383
public void List<TElement>(string notVidiblePropertyOrFieldName, Action<IListPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
372384
{
373-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
385+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
386+
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
387+
if (!typeof(TElement).Equals(collectionElementType))
388+
{
389+
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}",
390+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TElement).Name, collectionElementType.Name));
391+
}
374392
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
375393
RegisterListMapping<TElement>(collectionMapping, mapping, member, memberOf);
376394
}
@@ -382,7 +400,15 @@ public void List<TElement>(string notVidiblePropertyOrFieldName, Action<IListPro
382400

383401
public void Map<TKey, TElement>(string notVidiblePropertyOrFieldName, Action<IMapPropertiesMapper<TEntity, TKey, TElement>> collectionMapping, Action<IMapKeyRelation<TKey>> keyMapping, Action<ICollectionElementRelation<TElement>> mapping)
384402
{
385-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
403+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
404+
var propertyOrFieldType = member.GetPropertyOrFieldType();
405+
var keyType = propertyOrFieldType.DetermineDictionaryKeyType();
406+
var collectionElementType = propertyOrFieldType.DetermineDictionaryValueType();
407+
if (!typeof(TElement).Equals(collectionElementType) || !typeof(TKey).Equals(keyType))
408+
{
409+
throw new MappingException(string.Format("Wrong collection element type. For the property/field '{0}' of {1} was expected a dictionary of {2}/{3} but was {4}/{5}",
410+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TKey).Name, keyType.Name ,typeof(TElement).Name, collectionElementType.Name));
411+
}
386412
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
387413
RegisterMapMapping<TKey, TElement>(collectionMapping, keyMapping, mapping, member, memberOf);
388414
}
@@ -399,7 +425,13 @@ public void Map<TKey, TElement>(string notVidiblePropertyOrFieldName, Action<IMa
399425

400426
public void IdBag<TElement>(string notVidiblePropertyOrFieldName, Action<IIdBagPropertiesMapper<TEntity, TElement>> collectionMapping, Action<ICollectionElementRelation<TElement>> mapping)
401427
{
402-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
428+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
429+
var collectionElementType = member.GetPropertyOrFieldType().DetermineCollectionElementType();
430+
if (!typeof(TElement).Equals(collectionElementType))
431+
{
432+
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}",
433+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TElement).Name, collectionElementType.Name));
434+
}
403435
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
404436
RegisterIdBagMapping<TElement>(collectionMapping, mapping, member, memberOf);
405437
}
@@ -411,14 +443,26 @@ public void IdBag<TElement>(string notVidiblePropertyOrFieldName, Action<IIdBagP
411443

412444
public void ManyToOne<TProperty>(string notVidiblePropertyOrFieldName, Action<IManyToOneMapper> mapping) where TProperty : class
413445
{
414-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
446+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
447+
var propertyOrFieldType = member.GetPropertyOrFieldType();
448+
if (!typeof(TProperty).Equals(propertyOrFieldType))
449+
{
450+
throw new MappingException(string.Format("Wrong relation type. For the property/field '{0}' of {1} was expected a many-to-one with {2} but was {3}",
451+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TProperty).Name, propertyOrFieldType.Name));
452+
}
415453
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
416454
RegisterManyToOneMapping<TProperty>(mapping, member, memberOf);
417455
}
418456

419457
public void Component<TComponent>(string notVidiblePropertyOrFieldName, Action<IComponentMapper<TComponent>> mapping) where TComponent : class
420458
{
421-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
459+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
460+
var propertyOrFieldType = member.GetPropertyOrFieldType();
461+
if (!typeof(TComponent).Equals(propertyOrFieldType))
462+
{
463+
throw new MappingException(string.Format("Wrong relation type. For the property/field '{0}' of {1} was expected a component of {2} but was {3}",
464+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TComponent).Name, propertyOrFieldType.Name));
465+
}
422466
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
423467
RegisterComponentMapping<TComponent>(mapping, member, memberOf);
424468
}
@@ -430,23 +474,45 @@ public void Component<TComponent>(string notVidiblePropertyOrFieldName) where TC
430474

431475
public void Component<TComponent>(string notVidiblePropertyOrFieldName, TComponent dynamicComponentTemplate, Action<IDynamicComponentMapper<TComponent>> mapping) where TComponent : class
432476
{
433-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
477+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
434478
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
435479
RegisterDynamicComponentMapping<TComponent>(mapping, member, memberOf);
436480
}
437481

438482
public void Any<TProperty>(string notVidiblePropertyOrFieldName, System.Type idTypeOfMetaType, Action<IAnyMapper> mapping) where TProperty : class
439483
{
440-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
484+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
485+
var propertyOrFieldType = member.GetPropertyOrFieldType();
486+
if (!typeof(TProperty).Equals(propertyOrFieldType))
487+
{
488+
throw new MappingException(string.Format("Wrong relation type. For the property/field '{0}' of {1} was expected a heterogeneous (any) of type {2} but was {3}",
489+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TProperty).Name, propertyOrFieldType.Name));
490+
}
441491
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
442492
RegisterAnyMapping<TProperty>(mapping, idTypeOfMetaType, member, memberOf);
443493
}
444494

445495
public void OneToOne<TProperty>(string notVidiblePropertyOrFieldName, Action<IOneToOneMapper> mapping) where TProperty : class
446496
{
447-
MemberInfo member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
497+
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVidiblePropertyOrFieldName);
498+
var propertyOrFieldType = member.GetPropertyOrFieldType();
499+
if (!typeof(TProperty).Equals(propertyOrFieldType))
500+
{
501+
throw new MappingException(string.Format("Wrong relation type. For the property/field '{0}' of {1} was expected a one-to-one with {2} but was {3}",
502+
notVidiblePropertyOrFieldName, typeof(TEntity).FullName, typeof(TProperty).Name, propertyOrFieldType.Name));
503+
}
448504
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
449505
RegisterOneToOneMapping<TProperty>(mapping, member, memberOf);
450506
}
507+
508+
public static MemberInfo GetPropertyOrFieldMatchingNameOrThrow(string memberName)
509+
{
510+
var result = typeof(TEntity).GetPropertyOrFieldMatchingName(memberName);
511+
if (result == null)
512+
{
513+
throw new MappingException(string.Format("Member not found. The member '{0}' does not exists in type {1}", memberName, typeof(TEntity).FullName));
514+
}
515+
return result;
516+
}
451517
}
452518
}

src/NHibernate/Mapping/ByCode/SimpleModelInspector.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ protected bool MatchComponentPattern(System.Type subject)
214214
BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
215215

216216
var modelInspector = (IModelInspector) this;
217-
return !subject.IsEnum && !subject.Namespace.StartsWith("System") /* hack */&& !modelInspector.IsEntity(subject)
218-
&&
219-
!subject.GetProperties(flattenHierarchyMembers).Cast<MemberInfo>().Concat(
220-
subject.GetFields(flattenHierarchyMembers)).Any(m => modelInspector.IsPersistentId(m));
217+
return !subject.IsEnum && (subject.Namespace == null || !subject.Namespace.StartsWith("System")) /* hack */
218+
&& !modelInspector.IsEntity(subject)
219+
&& !subject.GetProperties(flattenHierarchyMembers).Cast<MemberInfo>().Concat(
220+
subject.GetFields(flattenHierarchyMembers)).Any(m => modelInspector.IsPersistentId(m));
221221
}
222222

223223
protected bool MatchEntity(System.Type subject)

0 commit comments

Comments
 (0)