Exemplo n.º 1
0
        /// <summary>
        /// Merges another <see cref="MapperContext"/> with this instance of <see cref="MapperContext"/>.
        /// </summary>
        /// <remarks>
        /// This will modify current object.
        /// </remarks>
        /// <param name="mapperContext"></param>
        /// <param name="mergeBehavior"></param>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public void MergeWith(MapperContext mapperContext,
                              MapperContextMergeBehavior mergeBehavior = MapperContextMergeBehavior.ThrowException)
        {
            switch (mergeBehavior)
            {
            case MapperContextMergeBehavior.OverwriteDuplicates:
            {
                Lock.EnterWriteLock();

                try
                {
                    foreach (var row in mapperContext.MappingRows)
                    {
                        MappingRows[row.Key] = row.Value;
                    }
                }
                finally
                {
                    Lock.ExitWriteLock();
                }

                break;
            }

            case MapperContextMergeBehavior.SkipDuplicates:
            {
                Lock.EnterWriteLock();

                try
                {
                    foreach (var row in mapperContext.MappingRows)
                    {
                        if (!MappingRows.ContainsKey(row.Key))
                        {
                            MappingRows[row.Key] = row.Value;
                        }
                    }
                }
                finally
                {
                    Lock.ExitWriteLock();
                }

                break;
            }

            case MapperContextMergeBehavior.ThrowException:
            {
                Lock.EnterReadLock();

                try
                {
                    foreach (var row in mapperContext.MappingRows)
                    {
                        if (MappingRows.ContainsKey(row.Key))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
                finally
                {
                    Lock.ExitReadLock();
                }

                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(mergeBehavior), mergeBehavior, null);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a new instance of <see cref="MapperScope"/> using given parent and context.
 /// </summary>
 /// <param name="parentScope">The parent scope.</param>
 /// <param name="context">The context to use in this scope.</param>
 public MapperScope(MapperScope parentScope, MapperContext context)
 {
     Context     = context ?? throw new ArgumentNullException(nameof(context));
     ParentScope = parentScope;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a new instance of <see cref="MapperScope"/> using an empty context.
 /// </summary>
 public MapperScope()
 {
     Context = new MapperContext();
 }
Exemplo n.º 4
0
 private static Expression CreateClassMapping(MapperContext context, Type destinationClassType,
                                              Type sourceClassType)
 {
     return(null);
 }
Exemplo n.º 5
0
 private static Expression DestinationFromValueResolver(MapperContext context, Expression destination,
                                                        Expression source,
                                                        Mapping.MemberMapInfo memberMapInfo)
 {
     return(null);
 }
Exemplo n.º 6
0
            public static MappingCompiledInfo CompileMapping(
                Mapper mapper,
                MapperContext context,
                MappingCompiledInfo compiledInfo,
                Mapping mapping,
                MappingType mappingType)
            {
                _ensureCompilers();

                var compilerContext = new Context(mapper, context, mapping, compiledInfo);

                var source = Expression.Parameter(mapping.SourceType, "source");

                switch (mappingType)
                {
                case MappingType.ObjectMap:
                {
                    if (compiledInfo.ObjectMapExpression != null)
                    {
                        return(compiledInfo);
                    }

                    break;
                }

                case MappingType.QueryableProjection:
                {
                    if (compiledInfo.ProjectionExpression != null)
                    {
                        return(compiledInfo);
                    }

                    foreach (var compiler in Compilers)
                    {
                        if (compiler.CanMap(compilerContext, mapping.SourceType, mapping.DestinationType))
                        {
                            var exp = compiler.CreateConvertExpression(
                                compilerContext,
                                source,
                                mapping.DestinationType,
                                mappingType
                                );

                            compiledInfo.ProjectionExpression = Expression.Lambda(exp, source);
                        }
                    }

                    break;
                }

                case MappingType.CollectionSynchronization:
                {
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException(nameof(mappingType), mappingType, null);
                }

                return(compiledInfo);
            }