2

这是我第一次使用 EmitMapper。我有一个对象列表,例如:客户,我想将此列表映射到 CustomerDTO 的可枚举中,我该怎么做?肿瘤坏死因子

4

2 回答 2

2

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

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

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

问题是当列表位于另一个对象中时,例如 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))
        );
于 2012-09-05T09:54:34.573 回答
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());

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

于 2016-10-24T14:11:14.963 回答