Example #1
0
 private void LogFoundMapping(TargetMap targetMap)
 {
     _logger.LogDebug(MappingLogEvents.MappingApplied, "Mapping Applied: {SourceType} --> {TargetType} Using Strategy: {StrategyType}",
                      targetMap.SourceType,
                      targetMap.TargetType,
                      targetMap.StrategyType);
 }
Example #2
0
        public object Map(object source, Type targetType)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source),
                                                "Source object to map cannot be null.");
            }

            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType),
                                                "Target Type to map source to cannot be null.");
            }

            TargetMap targetMap = FindMappingStrategy(source.GetType(), targetType);

            // Complete a reverse lookup.
            if (targetMap == null)
            {
                targetMap = FindMappingStrategy(targetType, source.GetType());
            }

            if (targetMap == null)
            {
                throw new InvalidOperationException(
                          $"Mapping strategy not found.  Source: { source.GetType().FullName} Target: {targetType.FullName}");
            }

            // If the mapping strategy instance was originally created by a IMappingStrategyFactory,
            // return the cached instance.  Otherwise, create an instance of the custom mapping strategy
            // using the service provider..
            var strategy = targetMap.StrategyInstance ??
                           (IMappingStrategy)_services.GetRequiredService(targetMap.StrategyType);

            LogFoundMapping(targetMap);
            return(strategy.Map(this, source));
        }