Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper DynamicMap with DataReader against an interface

I am relatively new to AutoMapper but found it very powerful in transforming my objects within a WCF service into DTOs (kind of the point).

Today I ran into a situation that I thought should work but fails. I am programming against interfaces in most places and a lot of my maps (that work) actually map EF entities to my DTO's interfaces (i.e. Mapper.CreateMap();).

However I ran into a situation where I have a datatable and I wanted to use AutoMapper to convert it. I quickly found some samples that looked promising but they kept failing with relatively vague exception (at least to me).

As a test, I changed the DynamicMap call to use the concrete implementation of the DTO and it worked successfully. I initially didn't go this route because all of the maps I created previously against interfaces which are resolved by my IoC container (Unity 3.x) worked.

Is there a way to get this particular map to work using the interface or do I have to use the concrete class when doing dynamic maps that return collections?

Failing:

AutoMapper.Mapper.DynamicMap<IDataReader, IEnumerable<IPunctuationMapDto>>(table.CreateDataReader());

Working:

AutoMapper.Mapper.DynamicMap<IDataReader, IEnumerable<PunctuationMapDto>>(table.CreateDataReader());

PunctuationMapperDto:

[DataContract]
public class PunctuationMapDto : IPunctuationMapDto
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string TaggedValue { get; set; }

    [DataMember]
    public int UntaggedValue { get; set; }
}

IPunctuationMapDto:

public interface IPunctuationMapDto
{
    int Id { get; set; }


    string TaggedValue { get; set; }


    int UntaggedValue { get; set; }
}
like image 737
James Legan Avatar asked Dec 03 '25 12:12

James Legan


1 Answers

Jimmy Bogard, AutoMapper creator replied in a different forum:

The data reader mapper doesn't support interfaces, only concrete classes. This is because the data reader mapper is implemented using reflection.emit for speed.

like image 164
James Legan Avatar answered Dec 05 '25 03:12

James Legan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!