AutoMapper Open Generics映射不起作用

时间:2017-03-03 15:28:30

标签: c# generics automapper nested-generics open-generics

我正在尝试设置地图以利用开放式泛型,但它从未在运行时运行。我在.NET Core中使用AutoMapper 5.2。

我有这些模特:

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;
}

我希望能够从一个RestData&lt; T&gt;映射到另一个RestData&lt; T&gt;其中T不是必需的。我创建一个看起来像这样的AutoMapper.Profile(使用接口):

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

我也尝试过这样(使用具体类型):

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

这就是我的RestDataConverter的样子:

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;
    }
}

我正在尝试在特定对象类型的两个集合之间进行映射(源:RestData&lt; List&lt; DocumentRecord&gt;&gt;,dest:RestData&lt; List&lt; Document&gt;&gt;)。以下是我的模型类型:

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; }
}

以下是这两种对象类型的AutoMapper.Profile:

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

我在Startup.cs中加载配置文件:

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

每当我做地图时,我都会遇到这个例外:

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]]]'.

此外,当我尝试做一些更简单的事情(来源:RestData&lt; int&gt;,dest:RestData&lt; int&gt;)时,比如这个单元测试,我得到了一个类似的例外:

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);
    }
}

相同的基本例外:

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]]'.

1 个答案:

答案 0 :(得分:0)

我弄清楚问题是什么。我的RestDataConverter使用的是接口类型而不是具体类型。当我改为使用具体的RestData&lt; T&gt;时,它突然开始正常工作。

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;
    }
}