Example #1
0
        public void CreateAccessorChain_SingleElement()
        {
            var chain = MapperUtils.CreateAccessorChain(MemberExpressions.GetExpressionChain <ClassWithSeveralPropertiesDest>(c => c.Child));

            Assert.IsNotNull(chain);
            var destination = new ClassWithSeveralPropertiesDest();
            var child       = new ChildClass();

            chain.Set(destination, child);
            Assert.AreSame(child, destination.Child);
        }
Example #2
0
        public override MapperAction <TContext> BuildAction <TFrom, TTo>(IMappingCollection <TFrom, TTo, TContext> map)
        {
            ExportMapInformation(map);

            MemberSetterAction <TContext> action = null;

            foreach (var iteratingSetter in map.Setters.Where(s => !s.IsIgnored))
            {
                var setter = iteratingSetter;
                if (setter.Remap)
                {
                    _mapper.RequireOneWayMap(setter.SourceType, setter.DestinationType, typeof(TFrom), typeof(TTo));
                }
                var toSetter   = setter.DestinationMember.CreateConstructingAccessorChain <TContext>(_mapper);
                var toAccessor = MapperUtils.CreateAccessorChain(setter.DestinationMember);
                switch (setter.SourceObjectType)
                {
                case MemberEntryType.Function:
                    var sourceFunc = (MapperAction <TContext>)setter.SourceFunc;
                    if (setter.Remap)
                    {
                        var remapper = _mapper.GetMapper(setter.SourceType, setter.DestinationType);
                        action += (from, to, context) => {
                            var dest = toAccessor.Get(to);
                            toSetter(to, remapper.MapObject(sourceFunc(from, to, context), dest, context), context);
                        };
                    }
                    else
                    {
                        action += (from, to, context) => toSetter(to, sourceFunc(from, to, context), context);
                    }
                    break;

                case MemberEntryType.Member:
                    var fromAccessor = MapperUtils.CreateAccessorChain(setter.SourceRoot.Union(setter.SourceMember));
                    if (setter.Remap)
                    {
                        var remapper = _mapper.GetMapper(setter.SourceType, setter.DestinationType);
                        action += (from, to, context) => toSetter(to, remapper.MapObject(fromAccessor.Get(from), toAccessor.Get(to), context), context);
                    }
                    else
                    {
                        action += (from, to, context) => toSetter(to, fromAccessor.Get(from), context);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException("MemberEntryType not supported");
                }
            }

            if (action == null)
            {
                return((from, to, context) => to);
            }
            else if (map.UpdatesContext)
            {
                return((from, to, context) =>
                {
                    if (to == null)
                    {
                        to = _mapper.ConstructOrThrow(typeof(TTo));
                    }
                    action(from, to, map.ContextUpdater(from, to, context));
                    return to;
                });
            }
            else
            {
                return((from, to, context) =>
                {
                    if (to == null)
                    {
                        to = _mapper.ConstructOrThrow(typeof(TTo));
                    }
                    action(from, to, context);
                    return to;
                });
            }
        }
Example #3
0
 public void CreateAccessorChain_NoElements_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() => MapperUtils.CreateAccessorChain(new MemberInfo[0]));
 }
Example #4
0
 public void CreateAccessorChain_Expression()
 {
     Assert.IsNotNull(MapperUtils.CreateAccessorChain <ClassWithSeveralPropertiesDest, ChildClass>(c => c.Child));
 }