@@ -55,7 +55,7 @@ protected virtual void RegisterNoVisiblePropertyMapping(string notVidiblePropert
55
55
{
56
56
// even seems repetitive, before unify this registration with the registration using Expression take in account that reflection operations
57
57
// done unsing expressions are faster than those done with pure reflection.
58
- MemberInfo member = typeof ( TEntity ) . GetPropertyOrFieldMatchingName ( notVidiblePropertyOrFieldName ) ;
58
+ MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow ( notVidiblePropertyOrFieldName ) ;
59
59
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
60
60
RegistePropertyMapping ( mapping , member , memberOf ) ;
61
61
}
@@ -346,7 +346,13 @@ protected virtual void RegisterIdBagMapping<TElement>(Action<IIdBagPropertiesMap
346
346
347
347
public void Set < TElement > ( string notVidiblePropertyOrFieldName , Action < ISetPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
348
348
{
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
+ }
350
356
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
351
357
RegisterSetMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
352
358
}
@@ -358,7 +364,13 @@ public void Set<TElement>(string notVidiblePropertyOrFieldName, Action<ISetPrope
358
364
359
365
public void Bag < TElement > ( string notVidiblePropertyOrFieldName , Action < IBagPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
360
366
{
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
+ }
362
374
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
363
375
RegisterBagMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
364
376
}
@@ -370,7 +382,13 @@ public void Bag<TElement>(string notVidiblePropertyOrFieldName, Action<IBagPrope
370
382
371
383
public void List < TElement > ( string notVidiblePropertyOrFieldName , Action < IListPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
372
384
{
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
+ }
374
392
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
375
393
RegisterListMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
376
394
}
@@ -382,7 +400,15 @@ public void List<TElement>(string notVidiblePropertyOrFieldName, Action<IListPro
382
400
383
401
public void Map < TKey , TElement > ( string notVidiblePropertyOrFieldName , Action < IMapPropertiesMapper < TEntity , TKey , TElement > > collectionMapping , Action < IMapKeyRelation < TKey > > keyMapping , Action < ICollectionElementRelation < TElement > > mapping )
384
402
{
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
+ }
386
412
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
387
413
RegisterMapMapping < TKey , TElement > ( collectionMapping , keyMapping , mapping , member , memberOf ) ;
388
414
}
@@ -399,7 +425,13 @@ public void Map<TKey, TElement>(string notVidiblePropertyOrFieldName, Action<IMa
399
425
400
426
public void IdBag < TElement > ( string notVidiblePropertyOrFieldName , Action < IIdBagPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
401
427
{
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
+ }
403
435
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
404
436
RegisterIdBagMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
405
437
}
@@ -411,14 +443,26 @@ public void IdBag<TElement>(string notVidiblePropertyOrFieldName, Action<IIdBagP
411
443
412
444
public void ManyToOne < TProperty > ( string notVidiblePropertyOrFieldName , Action < IManyToOneMapper > mapping ) where TProperty : class
413
445
{
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
+ }
415
453
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
416
454
RegisterManyToOneMapping < TProperty > ( mapping , member , memberOf ) ;
417
455
}
418
456
419
457
public void Component < TComponent > ( string notVidiblePropertyOrFieldName , Action < IComponentMapper < TComponent > > mapping ) where TComponent : class
420
458
{
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
+ }
422
466
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
423
467
RegisterComponentMapping < TComponent > ( mapping , member , memberOf ) ;
424
468
}
@@ -430,23 +474,45 @@ public void Component<TComponent>(string notVidiblePropertyOrFieldName) where TC
430
474
431
475
public void Component < TComponent > ( string notVidiblePropertyOrFieldName , TComponent dynamicComponentTemplate , Action < IDynamicComponentMapper < TComponent > > mapping ) where TComponent : class
432
476
{
433
- MemberInfo member = typeof ( TEntity ) . GetPropertyOrFieldMatchingName ( notVidiblePropertyOrFieldName ) ;
477
+ MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow ( notVidiblePropertyOrFieldName ) ;
434
478
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
435
479
RegisterDynamicComponentMapping < TComponent > ( mapping , member , memberOf ) ;
436
480
}
437
481
438
482
public void Any < TProperty > ( string notVidiblePropertyOrFieldName , System . Type idTypeOfMetaType , Action < IAnyMapper > mapping ) where TProperty : class
439
483
{
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
+ }
441
491
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
442
492
RegisterAnyMapping < TProperty > ( mapping , idTypeOfMetaType , member , memberOf ) ;
443
493
}
444
494
445
495
public void OneToOne < TProperty > ( string notVidiblePropertyOrFieldName , Action < IOneToOneMapper > mapping ) where TProperty : class
446
496
{
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
+ }
448
504
MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
449
505
RegisterOneToOneMapping < TProperty > ( mapping , member , memberOf ) ;
450
506
}
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
+ }
451
517
}
452
518
}
0 commit comments