映射IDataReader时没有调用AutoMapper TypeConverter



我有几个模型,我想使用AutoMapper进行映射。Automapper被设置为从IDataReader映射到模型类。问题是我需要在映射器上设置一个ITypeConverter,这样我就可以为每个模型枚举有限的次数。我不想为每个模型创建继承ITypeConverter的许多类。

示例模型:

public class Customer: IModel
{
    public FirstName { get; set; }
    public LastName { get; set; }
}

Mapper类:

public static class AutoMappingConfig
{
    public static void Configure()
    {
        // I would have many other mappings like the one below. All taking an IDataReader and mapping to a model inheriting from IModel
        Mapper.CreateMap<IDataReader, Customer>()
            .ForMember(x => x.FirstName, o => o.MapFrom(s => s.GetString(s.GetOrdinal("first_name")))
            .ForMember(x => x.LastName, o => o.MapFrom(s => s.GetString(s.GetOrdinal("last_name"))));
        // I would have many of the following. All taking an IDataReader and mapping to an IEnumerable model object
        Mapper.CreateMap<IDataReader, IEnumerable<Customer>>().ConvertUsing<ModelConverter<Customer>>();
    }
}

转换器:

public class ModelConverter<T>: ITypeConverter<IDataReader, IEnumerable<T>> where T: IModel
{
    public IEnumerable<T> Convert(ResolutionContext context)
    {
        var dataReader = (IDataReader) context.SourceValue;
        var rowCount = 0;
        var collection = new List<T>();
        // The purpose for this ModelConverter is the a maximum row count of 10
        while (dataReader.Read() && rowCount < 10)
        {
            var item = Mapper.Map<IDataReader, T>(dataReader);
            collection.Add(item);
            rowCount++;
        }
        return collection;
    }
}

注意:问题不在于ModelConverter类,因为当我传入IDataReader时它永远不会被调用。我已经尝试了一个类似的映射系统与另一个类,它被调用并成功处理映射。

当我运行以下代码时,返回的值是具有空映射的项列表。ModelConverter类根本不会被调用。上述代码适用于IDataReader以外的任何输入。AutoMapper正在对IDataReader做一些特别的事情,但我不确定此时如何继续使用ITypeConverter。

var customers = Mapper.Map<IDataReader, IEnumerable<Customer>>(dataReader);

在上面的代码中,dataReader是IDataReader对象

数据阅读器目前不支持类型转换器,抱歉。