Exemplo n.º 1
0
 public override void OverrideMapping(IMappingCollection <TFrom, TTo, TContext> mapping)
 {
     foreach (var propertyInfo in mapping.Unmapped.Destination.ToArray())
     {
         mapping.IgnoreMember(propertyInfo);
     }
 }
Exemplo n.º 2
0
        public override void OverrideMapping(IMappingCollection <ClassWithSeveralPropertiesSrc, ClassWithSeveralPropertiesDest, TContext> mapping)
        {
            int stringValue = 0;

            mapping.Set(to => to.Property2, () => stringValue++);
            mapping.Set(to => to.Property1, () => stringValue++);
            mapping.Set(to => to.Property3, () => stringValue++);
        }
Exemplo n.º 3
0
 public void CreateMap <TFrom, TTo, TContext>(IMappingCollection <TFrom, TTo, TContext> mappers)
 {
     foreach (var resolver in mappers.MemberResolvers)
     {
         foreach (var pair in mappers.ResolveMap(resolver))
         {
             mappers.SetMember(pair.Key, pair.Value);
         }
     }
 }
Exemplo n.º 4
0
 public override void OverrideMapping(IMappingCollection <ClassWithSeveralPropertiesSrc, ClassWithSeveralPropertiesDest, CloneableTestContext> mapping)
 {
     base.OverrideMapping(mapping);
     mapping.SetChildContext((from, to, context) => context);
     mapping.Set(to => to.Child, (from, to, context) =>
     {
         Assert.AreNotSame(_originalContext, context);
         return(null);
     });
 }
 public override void OverrideMapping(IMappingCollection <ClassWithSeveralPropertiesSrc, ClassWithSeveralPropertiesDest, TContext> mapping)
 {
     if (_creators == null)
     {
         return;
     }
     mapping.MemberConsumers.Clear();
     foreach (var mapCreator in _creators)
     {
         mapping.MemberConsumers.Add(mapCreator);
     }
 }
Exemplo n.º 6
0
 public static MemberInfo ResolveSource <TFrom, TTo, TContext>(this IMappingCollection <TFrom, TTo, TContext> resolvers, MemberInfo dest, IEnumerable <MemberInfo> available)
 {
     foreach (var memberResolver in resolvers.MemberResolvers)
     {
         var resolver = memberResolver;
         var source   = available.FirstOrDefault(m => resolver.IsSourceFor(dest, m, resolvers));
         if (source != null)
         {
             return(source);
         }
     }
     return(null);
 }
Exemplo n.º 7
0
        protected void ExportMapInformation <TFrom, TTo>(IMappingCollection <TFrom, TTo, TContext> map)
        {
#if SILVERLIGHT
            // Under silverlight there is no easy way to dump this mapping data for now...
#else
            if (_mapper.DiagnosticsEnabled && !string.IsNullOrEmpty(_mapper.ExportedMapsDirectory))
            {
                var filename = string.Format("{0}_To_{1}.xml",
                                             typeof(TFrom).Name, typeof(TTo).Name);
                var mapEntry = new TypeToTypeMap();
                mapEntry.Members = map.Setters.Select(s => new MapMemberDescription {
                    order  = s.SetOrder,
                    name   = s.DestinationMember.ToMemberName(),
                    type   = s.DestinationType.FullName,
                    Source = s.IsIgnored ? null
                           : new MemberDescription {
                        name = s.SourceObjectType == MemberEntryType.Member ?
                               s.SourceMember.ToMemberName()
                                           : null,
                        type     = s.SourceType != null ? s.SourceType.FullName : null,
                        Function = s.SourceObjectType == MemberEntryType.Function ?
                                   "Custom Function"
                                           : null
                    },
                    remapped = s.Remap
                }).ToArray();

                var serializer = new XmlSerializer(mapEntry.GetType());
                using (var outputStream = XmlWriter.Create(Path.Combine(_mapper.ExportedMapsDirectory, filename),
                                                           new XmlWriterSettings {
                    Indent = true
                }))
                {
                    serializer.Serialize(outputStream, mapEntry);
                }
            }
#endif
        }
Exemplo n.º 8
0
 public override void OverrideMapping(IMappingCollection <TFrom, TTo, TContext> mapping)
 {
     base.OverrideMapping(mapping);
     _overrides(mapping);
 }
Exemplo n.º 9
0
 public static IMappingCollection <TFrom, TTo, TContext> AutomapOnly <TFrom, TTo, TContext>(this IMappingCollection <TFrom, TTo, TContext> mappingCollection)
 {
     mappingCollection.DoAutomapping();
     foreach (var memberInfo in mappingCollection.Unmapped.Destination)
     {
         mappingCollection.IgnoreMember(memberInfo);
     }
     return(mappingCollection);
 }
Exemplo n.º 10
0
 public static IList <KeyValuePair <MemberInfo, MemberInfo> > ResolveMap <TFrom, TTo, TContext>(this IMappingCollection <TFrom, TTo, TContext> resolvers, IMemberResolver resolver)
 {
     return(resolvers.Unmapped.Destination.Select(d => new KeyValuePair <MemberInfo, MemberInfo>(d, resolvers.Unmapped.Source.FirstOrDefault(s => resolver.IsSourceFor(d, s, resolvers))))
            .Where(e => e.Value != null)
            .ToList());
 }
Exemplo n.º 11
0
 public static MemberInfo ResolveSource <TFrom, TTo, TContext>(this IMappingCollection <TFrom, TTo, TContext> resolvers, MemberInfo dest)
 {
     return(resolvers.ResolveSource(dest, resolvers.Unmapped.Source.ToArray()));
 }
Exemplo n.º 12
0
 public virtual void OverrideMapping(IMappingCollection <TFrom, TTo, TContext> mapping)
 {
 }
Exemplo n.º 13
0
 public abstract MapperAction <TContext> BuildAction <TFrom, TTo>(IMappingCollection <TFrom, TTo, TContext> map);
Exemplo n.º 14
0
 public override void OverrideMapping(IMappingCollection <ResourceClassSimple, DomainClassSimple, TContext> mapping)
 {
     mapping.Ignore(to => to.RandomProperty);
 }
Exemplo n.º 15
0
 public void OverrideMapping(IMappingCollection <TType2, TType1, TContext> mapping)
 {
     _bothDirectionsMap(new BidirectionMapWrapper <TType1, TType2, TContext>(mapping));
     _action(mapping);
 }
Exemplo n.º 16
0
 protected virtual void Override_TType2_To_TType1(IMappingCollection <TType2, TType1, TContext> mapping)
 {
 }
Exemplo n.º 17
0
 public BidirectionMapWrapper(IMappingCollection <TType2, TType1, TContext> mapping)
 {
     _reverseMapping = mapping;
 }
Exemplo n.º 18
0
 public bool IsSourceFor <TFrom, TTo, TContext>(MemberInfo dest, MemberInfo source, IMappingCollection <TFrom, TTo, TContext> mappers)
 {
     return(string.Equals(dest.Name, source.Name, StringComparison.CurrentCultureIgnoreCase));
 }
Exemplo n.º 19
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;
                });
            }
        }
Exemplo n.º 20
0
        public override MapperAction <TContext> BuildAction <TFrom, TTo>(IMappingCollection <TFrom, TTo, TContext> map)
        {
            ExportMapInformation(map);

            var convertMethod = GetOrCreateConvertor(typeof(TFrom), typeof(TTo));
            var context       = new CompilationContext(convertMethod.GetILGenerator());

            new AstWriteArgument(1, typeof(TTo), new AstIfNull(
                                     (IAstRef)AstBuildHelper.ReadArgumentRA(1, typeof(TTo)),
                                     AstBuildHelper.CastClass(AstBuildHelper.CallMethod(GetConstructOrThrowMethod(),
                                                                                        AstBuildHelper.ReadFieldRA(null, _mapperField),
                                                                                        new List <IAstStackItem> {
                new AstTypeof {
                    type = typeof(TTo)
                }
            }), typeof(TTo))
                                     )).Compile(context);

            if (map.UpdatesContext)
            {
                var funcField = _type.DefineField(GetFieldName <TFrom, TTo>(),
                                                  typeof(Func <object, object, TContext, TContext>),
                                                  FieldAttributes.Public | FieldAttributes.Static);
                _constructorValues.Add(funcField.Name, map.ContextUpdater);
//                        var sourceFuncRoot = setter.SourceRoot.Length > 0 ?
//                            AstBuildHelper.ReadMembersChain(AstBuildHelper.ReadArgumentRA(0, typeof(TFrom)), setter.SourceRoot)
//                            : AstBuildHelper.ReadArgumentRV(0, typeof(TFrom));
                var method = funcField.FieldType.GetMethod("Invoke",
                                                           new [] { typeof(object), typeof(object), typeof(TContext) });

                var contextUpdater = AstBuildHelper.CallMethod(
                    method,
                    AstBuildHelper.ReadFieldRA(null, funcField),
                    new List <IAstStackItem> {
                    AstBuildHelper.ReadArgumentRV(0, typeof(TFrom)),
                    AstBuildHelper.ReadArgumentRA(1, typeof(TTo)),
                    AstBuildHelper.ReadArgumentRA(2, typeof(TContext))
                });

                new AstWriteArgument(2, typeof(TContext), contextUpdater).Compile(context);
            }

            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));
                }
                switch (setter.SourceObjectType)
                {
                case MemberEntryType.Function:
                    var funcField = _type.DefineField(GetFieldName <TFrom, TTo>(), setter.SourceFunc.GetType(),
                                                      FieldAttributes.Public | FieldAttributes.Static);
                    _constructorValues.Add(funcField.Name, setter.SourceFunc);
//                        var sourceFuncRoot = setter.SourceRoot.Length > 0 ?
//                            AstBuildHelper.ReadMembersChain(AstBuildHelper.ReadArgumentRA(0, typeof(TFrom)), setter.SourceRoot)
//                            : AstBuildHelper.ReadArgumentRV(0, typeof(TFrom));
                    var method     = funcField.FieldType.GetMethod("Invoke", new [] { typeof(object), typeof(object), typeof(TContext) });
                    var sourceFunc = AstBuildHelper.CallMethod(
                        method,
                        AstBuildHelper.ReadFieldRA(null, funcField),
                        new List <IAstStackItem> {
                        AstBuildHelper.ReadArgumentRV(0, typeof(TFrom)),
                        AstBuildHelper.ReadArgumentRA(1, typeof(TTo)),
                        AstBuildHelper.ReadArgumentRA(2, typeof(TContext))
                    });
                    if (setter.Remap)
                    {
                        var remapper    = GetOrCreateMapper(setter.SourceType, setter.DestinationType);
                        var remapMethod = AstBuildHelper.CallMethod(GetConvertMethod(setter.SourceType, setter.DestinationType),
                                                                    AstBuildHelper.ReadFieldRA(null, remapper),
                                                                    new List <IAstStackItem> {
                            sourceFunc,
                            AstBuildHelper.ReadMembersChain(AstBuildHelper.ReadArgumentRA(1, typeof(TTo)), setter.DestinationMember),
                            AstBuildHelper.ReadArgumentRA(2, typeof(TContext)),
                        });
                        var destination = AstBuildHelper.WriteMembersChain(setter.DestinationMember,
                                                                           AstBuildHelper.ReadArgumentRA(1, typeof(TTo)),
                                                                           remapMethod);
                        destination.Compile(context);
                    }
                    else
                    {
                        var destination = AstBuildHelper.WriteMembersChain(setter.DestinationMember,
                                                                           AstBuildHelper.ReadArgumentRA(1, typeof(TTo)),
                                                                           sourceFunc);
                        destination.Compile(context);
                    }
                    break;

                case MemberEntryType.Member:
                    var sourceMember = AstBuildHelper.ReadMembersChain(AstBuildHelper.ReadArgumentRA(0, typeof(TFrom)),
                                                                       setter.SourceRoot.Union(setter.SourceMember).ToArray());
                    if (setter.Remap)
                    {
                        var remapper    = GetOrCreateMapper(setter.SourceType, setter.DestinationType);
                        var remapMethod = AstBuildHelper.CallMethod(GetConvertMethod(setter.SourceType, setter.DestinationType),
                                                                    AstBuildHelper.ReadFieldRA(null, remapper),
                                                                    new List <IAstStackItem> {
                            sourceMember,
                            AstBuildHelper.ReadMembersChain(AstBuildHelper.ReadArgumentRA(1, typeof(TTo)), setter.DestinationMember),
                            AstBuildHelper.ReadArgumentRA(2, typeof(TContext)),
                        });
                        var destination = AstBuildHelper.WriteMembersChain(setter.DestinationMember,
                                                                           AstBuildHelper.ReadArgumentRA(1, typeof(TTo)),
                                                                           remapMethod);
                        destination.Compile(context);
                    }
                    else
                    {
                        var destination = AstBuildHelper.WriteMembersChain(setter.DestinationMember,
                                                                           AstBuildHelper.ReadArgumentRA(1, typeof(TTo)),
                                                                           sourceMember);
                        destination.Compile(context);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException(setter.SourceObjectType.ToString(), "MemberEntryType not supported");
                }
            }

            new AstReturn {
                returnValue = AstBuildHelper.ReadArgumentRV(1, typeof(TTo)), returnType = typeof(TTo)
            }.Compile(context);
            var name = convertMethod.Name;
            Func <TFrom, TTo, TContext, TTo> converter = null;

            return((from, to, contxt) => {
                if (converter == null)
                {
                    converter = (Func <TFrom, TTo, TContext, TTo>)Delegate.CreateDelegate(
                        typeof(Func <TFrom, TTo, TContext, TTo>), null,
                        _type.GetMethod(name));
                }
                return converter((TFrom)from, (TTo)to, (TContext)contxt);
            });
        }