Пример #1
0
        public static TDestination Map <TSource, TDestination, TDestinationElement>(TSource source, TDestination destination, ResolutionContext context)
            where TSource : class, IEnumerable
            where TDestination : class, IEnumerable
        {
            if (source == null && context.Mapper.ShouldMapSourceCollectionAsNull(context))
            {
                return(null);
            }

            var sourceElementType = TypeHelper.GetElementType(typeof(TSource), source);
            var destElementType   = typeof(TDestinationElement);

            source = source ?? (context.ConfigurationProvider.AllowNullDestinationValues
                ? ObjectCreator.CreateNonNullValue(typeof(TSource))
                : ObjectCreator.CreateObject(typeof(TSource))) as TSource;

            TDestination destEnumeration = (destination is IList && !(destination is Array))
                ? destination
                : (TDestination)ObjectCreator.CreateList(destElementType);

            var list = destEnumeration as IList <TDestinationElement>;

            list.Clear();

            foreach (var item in source)
            {
                list.Add((TDestinationElement)context.Mapper.Map(item, null, sourceElementType, destElementType, context));
            }

            return(destEnumeration);
        }
Пример #2
0
        public static TDestination Map <TSource, TDestination, TDestinationElement>(TSource source, TDestination destination, ResolutionContext context)
            where TSource : class, IEnumerable
            where TDestination : class, IEnumerable
        {
            if (source == null)
            {
                return(context.Mapper.ShouldMapSourceCollectionAsNull(context) ? null : new List <TDestinationElement>() as TDestination);
            }

            var destElementType = typeof(TDestinationElement);

            TDestination destEnumeration = (destination is IList && !(destination is Array))
                ? destination
                : (TDestination)ObjectCreator.CreateList(destElementType);

            var list = destEnumeration as IList <TDestinationElement>;

            list.Clear();
            var itemContext = new ResolutionContext(context);

            foreach (var item in source)
            {
                list.Add(itemContext.Map(item, default(TDestinationElement)));
            }
            return(destEnumeration);
        }
Пример #3
0
        protected override object GetOrCreateDestinationObject(ResolutionContext context, Type destElementType,
                                                               int sourceLength)
        {
            if (context.DestinationValue is IList && !(context.DestinationValue is Array))
            {
                return(context.DestinationValue);
            }

            return(ObjectCreator.CreateList(destElementType));
        }
Пример #4
0
        static IEnumerable LoadDataReaderViaList(IDataReader dataReader, IMappingEngineRunner mapper, Build buildFrom, ResolutionContext resolveUsingContext, Type elementType)
        {
            var list = ObjectCreator.CreateList(elementType);

            while (dataReader.Read())
            {
                var result = buildFrom(dataReader);
                MapPropertyValues(resolveUsingContext, mapper, result);
                list.Add(result);
            }

            return(list);
        }
Пример #5
0
        public object Map(ResolutionContext context, IMappingEngineRunner mapper)
        {
            if (IsDataReader(context))
            {
                var dataReader             = (IDataReader)context.SourceValue;
                var destinationElementType = TypeHelper.GetElementType(context.DestinationType);
                var resolveUsingContext    = context;
                if (context.TypeMap == null)
                {
                    var     configurationProvider = mapper.ConfigurationProvider;
                    TypeMap typeMap = configurationProvider.FindTypeMapFor(context.SourceValue, context.SourceType, destinationElementType);
                    resolveUsingContext = new ResolutionContext(typeMap, context.SourceValue, context.SourceType, destinationElementType);
                }
                var buildFrom = CreateBuilder(destinationElementType, dataReader);

                var results = ObjectCreator.CreateList(destinationElementType);
                while (dataReader.Read())
                {
                    var result = buildFrom(dataReader);
                    MapPropertyValues(resolveUsingContext, mapper, result);
                    results.Add(result);
                }

                return(results);
            }

            if (IsDataRecord(context))
            {
                var dataRecord = context.SourceValue as IDataRecord;
                var buildFrom  = CreateBuilder(context.DestinationType, dataRecord);

                var result = buildFrom(dataRecord);
                MapPropertyValues(context, mapper, result);

                return(result);
            }

            return(null);
        }
Пример #6
0
 protected override IList CreateDestinationObjectBase(Type destElementType, int sourceLength)
 {
     return(ObjectCreator.CreateList(destElementType));
 }