예제 #1
0
        public object ResolveValue(ResolutionContext context, IMappingEngineRunner mappingEngine)
        {
            var ctorArgs = new List<object>();

            foreach (var map in CtorParams)
            {
                var result = map.ResolveValue(context);

                var sourceType = result.Type;
                var destinationType = map.Parameter.ParameterType;

                var typeMap = mappingEngine.ConfigurationProvider.ResolveTypeMap(result, destinationType);

                Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType;

                var newContext = context.CreateTypeContext(typeMap, result.Value, null, targetSourceType,
                    destinationType);

                if (typeMap == null && map.Parameter.IsOptional)
                {
                    object value = map.Parameter.DefaultValue;
                    ctorArgs.Add(value);
                }
                else
                {
                    var value = mappingEngine.Map(newContext);
                    ctorArgs.Add(value);
                }
            }

            return _runtimeCtor.Value(ctorArgs.ToArray());
        }
예제 #2
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var typeMap = mapper.ConfigurationProvider.FindClosedGenericTypeMapFor(context);

            var newContext = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType,
                                                       context.DestinationType);

            return(mapper.Map(newContext));
        }
예제 #3
0
        public object Map(ResolutionContext context)
        {
            var typeMap = context.MapperContext.ConfigurationProvider.FindClosedGenericTypeMapFor(context);

            var newContext = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue,
                context.SourceType, context.DestinationType);

            return context.MapperContext.Runner.Map(newContext);
        }
예제 #4
0
 public object Map(ResolutionContext context)
 {
     var source = context.SourceValue;
     var sourceType = source.GetType();
     var sourceTypeDetails = new TypeDetails(sourceType, _ => true, _ => true);
     var membersDictionary = sourceTypeDetails.PublicReadAccessors.ToDictionary(p => p.Name, p => p.GetMemberValue(source));
     var newContext = context.CreateTypeContext(null, membersDictionary, context.DestinationValue, membersDictionary.GetType(), context.DestinationType);
     return context.Engine.Map(newContext);
 }
            public object Map(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var newSource = context.TypeMap.Substitution(context.SourceValue);
                var typeMap   = mapper.ConfigurationProvider.ResolveTypeMap(newSource.GetType(), context.DestinationType);

                var substitutionContext = context.CreateTypeContext(typeMap, newSource, context.DestinationValue,
                                                                    newSource.GetType(), context.DestinationType);

                return(mapper.Map(substitutionContext));
            }
예제 #6
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var source            = context.SourceValue;
            var sourceType        = source.GetType();
            var sourceTypeDetails = new TypeDetails(sourceType, _ => true, _ => true);
            var membersDictionary = sourceTypeDetails.PublicReadAccessors.ToDictionary(p => p.Name, p => p.GetMemberValue(source));
            var newContext        = context.CreateTypeContext(null, membersDictionary, context.DestinationValue, membersDictionary.GetType(), context.DestinationType);

            return(mapper.Map(newContext));
        }
            public object Map(ResolutionContext context, IMappingEngineRunner mapper)
            {
                var newSource = context.TypeMap.Substitution(context.SourceValue);
                var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(newSource.GetType(), context.DestinationType);

                var substitutionContext = context.CreateTypeContext(typeMap, newSource, context.DestinationValue,
                    newSource.GetType(), context.DestinationType);

                return mapper.Map(substitutionContext);
            }
                public List <ChildDto> Convert(ResolutionContext resolutionContext)
                {
                    int childId = (int)resolutionContext.SourceValue;
                    List <ChildModel> childModels = _childModels.Where(x => x.Parent.ID == childId).ToList();
                    var context = resolutionContext.CreateTypeContext(
                        null,
                        childModels, null, typeof(List <ChildModel>), typeof(List <ChildDto>));

                    return((List <ChildDto>)context.Engine.Map(context));
                }
                public ParentDto Convert(ResolutionContext resolutionContext)
                {
                    int         childId     = (int)resolutionContext.SourceValue;
                    ParentModel parentModel = _parentModels[childId];
                    var         context     = resolutionContext.CreateTypeContext(
                        resolutionContext.Engine.ConfigurationProvider.ResolveTypeMap(typeof(ParentModel), typeof(ParentDto)),
                        parentModel, null, typeof(ParentModel), typeof(ParentDto));

                    return((ParentDto)resolutionContext.Engine.Map(context));
                }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var contextTypePair = new TypePair(context.SourceType, context.DestinationType);
            Func<TypePair, IObjectMapper> missFunc = tp => context.Engine.ConfigurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(context));
            var typeMap = mapper.ConfigurationProvider.CreateTypeMap(context.SourceType, context.DestinationType, _profileName);

            context = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType);

            var map = (context.Engine as MappingEngine)._objectMapperCache.GetOrAdd(contextTypePair, missFunc);
            return map.Map(context, mapper);
        }
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var contextTypePair = new TypePair(context.SourceType, context.DestinationType);
            Func <TypePair, IObjectMapper> missFunc = tp => context.Engine.ConfigurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(context));
            var typeMap = mapper.ConfigurationProvider.CreateTypeMap(context.SourceType, context.DestinationType, _profileName);

            context = context.CreateTypeContext(typeMap, context.SourceValue, context.DestinationValue, context.SourceType, context.DestinationType);

            var map = context.Engine.GetOrAddMapper(contextTypePair, missFunc);

            return(map.Map(context, mapper));
        }
예제 #12
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            var container    = (ILifetimeScope)context.Options.ServiceCtor(typeof(ILifetimeScope));
            var cacheManager = container.Resolve <ICacheManager>(TypedParameter.From(GetType()));

            // create destination value by passing source value as IPagedList
            var createDestination = cacheManager.Get($"lambda-{GetType().Name}-create_{context.DestinationType.FullName}", acquireContext =>
            {
                var constructorInfo  = context.DestinationType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new[] { typeof(IPagedList) }, new ParameterModifier[0]);
                var parameter        = Expression.Parameter(typeof(object));
                var parameterConvert = Expression.Convert(parameter, typeof(IPagedList));
                var @new             = Expression.New(constructorInfo, parameterConvert);
                return(Expression.Lambda <Func <object, object> >(@new, parameter).Compile());
            });
            var destinationValue = createDestination(context.SourceValue);

            // mapping data object
            var sourceItemType                = context.SourceType.GenericTypeArguments[0];
            var destinationItemType           = context.DestinationType.GenericTypeArguments[0];
            var enumerableSourceItemType      = typeof(IEnumerable <>).MakeGenericType(sourceItemType);
            var enumerableDestinationItemType = typeof(IEnumerable <>).MakeGenericType(destinationItemType);
            var typeContext = context.CreateTypeContext(null, context.SourceValue, null, enumerableSourceItemType, enumerableDestinationItemType);
            var data        = mapper.Map(typeContext);

            // set data: using expression tree instead of reflection, and have the compiled lambda cached (much better performance)
            var setData = cacheManager.Get($"lambda-{GetType().Name}-set_data-from_{context.SourceType.FullName}-to_{context.DestinationType.FullName}", acquireContext =>
            {
                var instance        = Expression.Parameter(typeof(object), "model");
                var instanceConvert = Expression.Convert(instance, context.DestinationType);
                var value           = Expression.Parameter(typeof(object), "data");
                var valueConvert    = Expression.Convert(value, enumerableDestinationItemType);
                var property        = Expression.Property(instanceConvert, "Data");
                var setMethod       = ((PropertyInfo)property.Member).GetSetMethod();
                var call            = Expression.Call(instanceConvert, setMethod, valueConvert);
                return(Expression.Lambda <Action <object, object> >(call, instance, value).Compile());
            });

            setData(destinationValue, data); // set the value

            // reflection way to set data
//            var dataProp = context.DestinationType.GetProperty("Data");
//            dataProp.SetValue(destinationValue, data);

            return(destinationValue);
        }