Automapper打开通用映射无法正常工作



我正在尝试设置一个地图以利用开放式仿制药,但是它在运行时永远无法使用。我正在使用.NET Core.

我有这些模型:

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;不必必要的地方。我创建了一个看起来像这样的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<,>));
    }
}

这就是我的retdataConverter的样子:

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; d dest:restdata&lt; list&lt; locord&lt; gt; 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();
    }
}

我正在启动中加载配置文件:

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

每当我做地图时,我都会得到此例外:

无法施放类型的对象'restdataConverter`2 [system.collections.generic.list`1 [documentRecord],system.collections.generic.generic.list`1 [document'1 [document]''Restdata`1 [System.Collections.generic.list`1 [documentRecord],Restdata`1 [System.Collections.generic.list'1 [document]]]]'。

此外,当我尝试做一些更简单的事情(来源:restdata&gt; int 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);
    }
}

相同的基本异常:

无法施放类型的对象'restdataConverter`2 [system.int32,system.int32]'键入'autoMapper.itypeconverter`2 [restdata`1 [system.int.int32],restdata'1 [systdata'1 [system.int.int32]]'。

我弄清楚了问题是什么。我的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;
    }
}

相关内容

  • 没有找到相关文章

最新更新