public ObjectMapper <TSource, TTarget> Create <TSource, TTarget>(ObjectMappingData <TSource, TTarget> mappingData)
        {
            var mappingExpressionFactory = _mappingExpressionFactories.First(mef => mef.IsFor(mappingData));
            var mappingExpression        = mappingExpressionFactory.Create(mappingData);

            if (mappingExpression.NodeType == ExpressionType.Default)
            {
                return(null);
            }

            mappingExpression = MappingFactory
                                .UseLocalSourceValueVariableIfAppropriate(mappingExpression, mappingData.MapperData);

            var mappingLambda = Expression.Lambda <MapperFunc <TSource, TTarget> >(
                mappingExpression,
                mappingData.MapperData.MappingDataObject);

            var mapper = new ObjectMapper <TSource, TTarget>(mappingLambda, mappingData);

            if (_creationCallbacksByKey == null)
            {
                return(mapper);
            }

            var creationCallbackKey = new MapperCreationCallbackKey(mappingData.MapperData);

            if (_creationCallbacksByKey.TryGetValue(creationCallbackKey, out var creationCallback))
            {
                creationCallback.Invoke(mapper);
            }

            return(mapper);
        }
        public ObjectMapper <TSource, TTarget> Create <TSource, TTarget>(ObjectMappingData <TSource, TTarget> mappingData)
        {
            var mappingExpression = MappingDataSourceSetFactory.CreateFor(mappingData).BuildValue();

            if (mappingExpression == Constants.EmptyExpression)
            {
                return(null);
            }

            mappingExpression = MappingFactory
                                .UseLocalSourceValueVariableIfAppropriate(mappingExpression, mappingData.MapperData);

            var mapper = new ObjectMapper <TSource, TTarget>(mappingExpression, mappingData);

            if (_creationCallbacksByKey == null)
            {
                return(mapper);
            }

            var creationCallbackKey = new MapperCreationCallbackKey(mappingData.MapperData);

            if (_creationCallbacksByKey.TryGetValue(creationCallbackKey, out var creationCallback))
            {
                creationCallback.Invoke(mapper);
            }

            return(mapper);
        }
示例#3
0
        public ObjectMapper <TSource, TTarget> Create <TSource, TTarget>(ObjectMappingData <TSource, TTarget> mappingData)
        {
            var mappingExpression = mappingData.MapperKey.MappingTypes.IsEnumerable
                ? _enumerableMappingExpressionFactory.Create(mappingData)
                : _complexTypeMappingExpressionFactory.Create(mappingData);

            mappingExpression = MappingFactory
                                .UseLocalSourceValueVariableIfAppropriate(mappingExpression, mappingData.MapperData);

            var mappingLambda = Expression.Lambda <MapperFunc <TSource, TTarget> >(
                mappingExpression,
                mappingData.MapperData.MappingDataObject);

            var mapper = new ObjectMapper <TSource, TTarget>(mappingLambda, mappingData.MapperData);

            return(mapper);
        }