2

I am trying to setup a map to utilize open generics, but it never works at runtime. I'm using AutoMapper 5.2 in .NET Core.

I have these models:

public interface IRestData<T>
{
    T Data { get; }
    IPaging Paging { get; }

    void SetData(T data);
    void SetPaging(IPaging paging);
}

public interface IPaging
{
    int Count { get; }

    void SetCount(int count);
}

public class RestData<T> : IRestData<T>
{
    T _data;
    IPaging _paging = new Paging(0);

    public RestData() {}

    public RestData(T data)
    {
        _data = data;

        if (!typeof(IEnumerable).GetTypeInfo()
                                .IsAssignableFrom(typeof(T)))
            _paging = new Paging(data != null
                                     ? 1
                                     : 0);
    }

    public RestData(T data, IPaging paging)
    {
        _data = data;
        _paging = paging;
    }

    public T Data => _data;
    public IPaging Paging => _paging;

    public void SetData(T data) => _data = data;
    public void SetPaging(IPaging paging) => _paging = paging;
}

public class Paging : IPaging
{
    int _count;

    public Paging() {}

    public Paging(int count)
    {
        _count = count;
    }

    public int Count => _count;
    public void SetCount(int count) => _count = count;
}

I want to be able to Map from one RestData<T> to another RestData<T> where T is not necessarilly the same. I create an AutoMapper.Profile that looks like this (using interface):

public class CommonProfile : Profile
{
    public CommonProfile()
    {
        CreateMap(typeof(IRestData<>), typeof(IRestData<>))
            .ConvertUsing(typeof(RestDataConverter<,>));
    }
}

I also tried it like this (using concrete type):

public class CommonProfile : Profile
{
    public CommonProfile()
    {
        CreateMap(typeof(RestData<>), typeof(RestData<>))
            .ConvertUsing(typeof(RestDataConverter<,>));
    }
}

This is what my RestDataConverter looks like:

public class RestDataConverter<TSource, TDestination> : ITypeConverter<IRestData<TSource>, IRestData<TDestination>>
{
    public IRestData<TDestination> Convert(IRestData<TSource> source, IRestData<TDestination> destination, ResolutionContext context)
    {
        destination = destination ?? new RestData<TDestination>();
        destination.SetData(context.Mapper.Map<TDestination>(source.Data));
        destination.SetPaging(source.Paging);
        return destination;
    }
}

I'm trying to map between two collections of specific object types (source: RestData<List<DocumentRecord>>, dest: RestData<List<Document>>). Here are my model types:

public class DocumentRecord
{
    public DateTime CreatedTs { get; set; }
    public int DocumentId { get; set; }
    public long FileSize { get; set; }
    public DateTime LastUpdatedTs { get; set; }
    public int NumberOfPages { get; set; }
    public string OriginalFileName { get; set; }
    public IList<PageGroupRecord> PageGroups { get; set; } = new List<PageGroupRecord>();
    public string Type { get; set; }
}

public class Document
{
    public int ConfigurationId { get; set; }
    public DateTime CreatedTs { get; set; }
    public int DocumentId { get; set; }
    public string FileLocation { get; set; }
    public int FileSize { get; set; }
    public DateTime LastUpdatedTs { get; set; }
    public int NumberOfPages { get; set; }
    public string OriginalFileName { get; set; }
    public IList<PageGroup> PageGroups { get; set; } = new List<PageGroup>();
    public string Type { get; set; }
}

And here is the AutoMapper.Profile for these two object types:

public class ServicesProfile : Profile
{
    public ServicesProfile()
    {
        CreateMap<Document, DocumentRecord>()
            .ForMember(_ => _.Configuration, _ => _.Ignore())
            .ReverseMap();
    }
}

I am loading profiles in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    var mapperConfiguration = new MapperConfiguration(_ =>
                                                    {
                                                        _.AddProfile<CommonProfile>();
                                                        _.AddProfile<ServicesProfile>();
                                                    });
    services.AddSingleton(mapperConfiguration);
    services.AddSingleton(mapperConfiguration.CreateMapper());
}

Whenever I do a map I get this exception:

Unable to cast object of type 'RestDataConverter`2[System.Collections.Generic.List`1[DocumentRecord],System.Collections.Generic.List`1[Document]]' to type 'AutoMapper.ITypeConverter`2[RestData`1[System.Collections.Generic.List`1[DocumentRecord]],RestData`1[System.Collections.Generic.List`1[Document]]]'.

Furthermore, when I try to do something more simple (source: RestData<int>, dest: RestData<int>) such as this unit test, I get a similar exception:

public class CommonProfileTests : BaseTests
{
    static CommonProfileTests()
    {
        Mapper.Initialize(m => m.AddProfile<CommonProfile>());
    }

    // This unit test passes
    [Fact]
    public void Configuration_Is_Valid() => AssertConfigurationIsValid();

    // This unit test fails with the error below
    [Fact]
    public void RestData_Maps_To_RestData_Correctly()
    {
        var source = new RestData<int>(1, new Paging(4));

        var destination = Map<RestData<int>>(source);

        Assert.Equal(source.Data, destination.Data);
    }
}

Same basic exception:

Unable to cast object of type 'RestDataConverter`2[System.Int32,System.Int32]' to type 'AutoMapper.ITypeConverter`2[RestData`1[System.Int32],RestData`1[System.Int32]]'.
Ristogod
  • 905
  • 4
  • 14
  • 29

1 Answers1

1

I figured out what the issue was. My RestDataConverter was using the interface types instead of the concrete types. When I changed to using the concrete RestData<T>, it suddendly started working fine.

public class RestDataConverter<TSource, TDestination> : ITypeConverter<RestData<TSource>, RestData<TDestination>>
{
    public RestData<TDestination> Convert(RestData<TSource> source, RestData<TDestination> destination, ResolutionContext context)
    {
        destination = destination ?? new RestData<TDestination>();
        destination.SetData(context.Mapper.Map<TDestination>(source.Data));
        destination.SetPaging(source.Paging);
        return destination;
    }
}
Ristogod
  • 905
  • 4
  • 14
  • 29