11namespace AgileObjects . AgileMapper
22{
33 using System ;
4+ using System . Linq . Expressions ;
45 using System . Reflection ;
56#if SERIALIZATION_SUPPORTED
67 using System . Runtime . Serialization ;
78#endif
9+ using Extensions . Internal ;
810 using Members ;
911 using NetStandardPolyfills ;
10- using ObjectPopulation ;
1112
1213 /// <summary>
1314 /// Represents an error that occurred during a mapping.
1920 #endregion
2021 public class MappingException : Exception
2122 {
22- internal static readonly MethodInfo FactoryMethod =
23+ private static readonly MethodInfo _factoryMethod =
2324 typeof ( MappingException ) . GetPublicStaticMethod ( "For" ) ;
2425
25- internal const string NoMappingData = "An exception occurred creating a mapping data instance" ;
26-
2726 #region Serialization Support
2827#if SERIALIZATION_SUPPORTED
2928 /// <summary>
@@ -40,41 +39,44 @@ protected MappingException(SerializationInfo info, StreamingContext context)
4039#endif
4140 #endregion
4241
43- private MappingException ( IMemberMapperData mapperData , Exception innerException )
44- : base ( GetMessage ( mapperData ) , innerException )
42+ private MappingException ( string message , Exception innerException )
43+ : base ( message , innerException )
4544 {
4645 }
4746
4847 /// <summary>
4948 /// Creates a new instance of the MappingException class.
5049 /// </summary>
51- /// <typeparam name="TSource">The source type being mapped when the exception occurred.</typeparam>
52- /// <typeparam name="TTarget">The target type being mapped when the exception occurred.</typeparam>
53- /// <param name="mappingData">
54- /// The <see cref="IObjectMappingData{TSource, TTarget}"/> containing the mapping data of the
55- /// current mapping context.
56- /// </param>
50+ /// <param name="ruleSetName">The name of the mapping rule set being executed when the exception occurred.</param>
51+ /// <param name="sourcePath">The path of the source object being mapped when the exception occurred.</param>
52+ /// <param name="targetPath">The path of the target object being mapped when the exception occurred.</param>
5753 /// <param name="innerException">The exception which caused the creation of the MappingException.</param>
5854 /// <returns>A new MappingException instance.</returns>
59- public static MappingException For < TSource , TTarget > (
60- IObjectMappingData < TSource , TTarget > mappingData ,
55+ public static MappingException For (
56+ string ruleSetName ,
57+ string sourcePath ,
58+ string targetPath ,
6159 Exception innerException )
6260 {
63- return new MappingException ( ( ( IObjectMappingData ) mappingData ) ? . MapperData , innerException ) ;
61+ return new MappingException (
62+ $ "An exception occurred mapping { sourcePath } -> { targetPath } with rule set { ruleSetName } .",
63+ innerException ) ;
6464 }
6565
66- private static string GetMessage ( IMemberMapperData mapperData )
66+ internal static Expression GetFactoryMethodCall ( IMemberMapperData mapperData , Expression exceptionVariable )
6767 {
68- if ( mapperData == null )
69- {
70- return NoMappingData ;
71- }
72-
7368 var rootData = mapperData . GetRootMapperData ( ) ;
7469 var sourcePath = mapperData . SourceMember . GetFriendlySourcePath ( rootData ) ;
7570 var targetPath = mapperData . TargetMember . GetFriendlyTargetPath ( rootData ) ;
7671
77- return $ "An exception occurred mapping { sourcePath } -> { targetPath } with rule set { mapperData . RuleSet . Name } .";
72+ var mappingExceptionCreation = Expression . Call (
73+ _factoryMethod ,
74+ mapperData . RuleSet . NameConstant ,
75+ sourcePath . ToConstantExpression ( ) ,
76+ targetPath . ToConstantExpression ( ) ,
77+ exceptionVariable ) ;
78+
79+ return mappingExceptionCreation ;
7880 }
7981 }
8082}
0 commit comments