EmitMapper和List

时间:2012-03-05 14:22:57

标签: emitmapper

这是我第一次使用EmitMapper。 我有一个对象ex列表:客户,我想将此列表映射到CustomerDTO的无数个中,我该怎么做? TNX

2 个答案:

答案 0 :(得分:2)

如果您有一个列表并希望将其转换为DTO列表,这很简单:

 var mapper = ObjectMapperManager.DefaultInstance.GetMapper<Customer, CustomerDTO>();

 IEnumerable<CustomerDTO> dtos = listOfCustomer.Select(mapper.map);

preblem是列表在另一个对象中,例如User和UserDTO:

 class User { 
    public List<Customer> Customers { get; set; }
 }

 class UserDTO {
    public IEnumerable<CustomerDTO> Customers { get; set; }
 }

似乎EmitMapper不支持从List到Enumerable的转换。支持它的方法是:

      var customerMapper = ObjectMapperManager
            .DefaultInstance.GetMapper<Customer, CustomerDTO>();
      var mapper = ObjectMapperManager.DefaultInstance
            .GetMapper<User, UserDTO>(
               new DefaultMapConfig()
                 .ConvertUsing<List<Customer>, IEnumerable<CustomerDTO>>(
                     a => a.Select(customerMapper.Map))
        );

答案 1 :(得分:0)

这可以创建自定义类,实现接口“ICustomConverterProvider”并将ConvertGeneric添加到“DefaultMapConfig”。

查看EmitMapper的源代码,我发现了一个名为“ArraysConverterProvider”的类,它是从ICollections到Arrays的默认通用转换器。

调整此类中的代码以使用IEnumerable集合:

class GenericIEnumerableConverterProvider : ICustomConverterProvider
    {
      public CustomConverterDescriptor GetCustomConverterDescr(
            Type from,
            Type to,
            MapConfigBaseImpl mappingConfig)
        {
            var tFromTypeArgs = DefaultCustomConverterProvider.GetGenericArguments(from);
            var tToTypeArgs = DefaultCustomConverterProvider.GetGenericArguments(to);
            if (tFromTypeArgs == null || tToTypeArgs == null || tFromTypeArgs.Length != 1 || tToTypeArgs.Length != 1)
            {
                return null;
            }
            var tFrom = tFromTypeArgs[0];
            var tTo = tToTypeArgs[0];
            if (tFrom == tTo && (tFrom.IsValueType || mappingConfig.GetRootMappingOperation(tFrom, tTo).ShallowCopy))
           {
               return new CustomConverterDescriptor
                {
                    ConversionMethodName = "Convert",
                    ConverterImplementation = typeof(GenericIEnumerableConverter_OneTypes<>),
                    ConverterClassTypeArguments = new[] { tFrom }
                };
            }

            return new CustomConverterDescriptor
            {
                ConversionMethodName = "Convert",
                ConverterImplementation = typeof(GenericIEnumerableConverter_DifferentTypes<,>),
                ConverterClassTypeArguments = new[] { tFrom, tTo }
            }; 
        }
    }


class GenericIEnumerableConverter_DifferentTypes<TFrom, TTo> : ICustomConverter
    {
        private Func<TFrom, TTo> _converter;
        public IEnumerable<TTo> Convert(IEnumerable<TFrom> from, object state)
        {
            if (from == null)
            {
                return null;
            } 

            TTo[] result = new TTo[from.Count()];
            int idx = 0;
            foreach (var f in from)
            {
                result[idx++] = _converter(f);
            }

            return result;
        } 

        public void Initialize(Type from, Type to, MapConfigBaseImpl mappingConfig)
        {
            var staticConverters = mappingConfig.GetStaticConvertersManager() ?? StaticConvertersManager.DefaultInstance;

            var staticConverterMethod = staticConverters.GetStaticConverter(typeof(TFrom), typeof(TTo));

           if (staticConverterMethod != null)
            {
                _converter = (Func<TFrom, TTo>)Delegate.CreateDelegate(
                    typeof(Func<TFrom, TTo>),
                    null,
                    staticConverterMethod
                );
            }
            else
            {
                _subMapper = ObjectMapperManager.DefaultInstance.GetMapperImpl(typeof(TFrom), typeof(TTo), mappingConfig);

                _converter = ConverterBySubmapper;
            }
        } 

        ObjectsMapperBaseImpl _subMapper;

        private TTo ConverterBySubmapper(TFrom from)
        {
            return (TTo)_subMapper.Map(from);
        }
    }


class GenericIEnumerableConverter_OneTypes<T>
    {

        public IEnumerable<T> Convert(IEnumerable<T> from, object state)
        {
            if (from == null)
          {
                return null;
            }

            return from;
        }
    } 

此代码只是一个尽可能少适应的副本,可以应用于具有多层次结构的对象

您可以使用以下命令使用上述代码:

new DefaultMapConfig().ConvertGeneric(
typeof(IEnumerable<>),
typeof(IEnumerable<>),
new GenericIEnumerableConverterProvider());

这节省了我的一天,我也希望能救你的!呵呵呵