【问题标题】:Mapping with generic destination doesn't work?使用通用目的地进行映射不起作用?
【发布时间】:2019-02-21 08:37:59
【问题描述】:

我正在尝试构建映射配置文件的层次结构,以简化为适合该层次结构的新对象创建映射。但我无法让一些映射工作。我使用泛型类型参数添加的映射在目标类型时似乎不起作用,但在源类型时却有效。

考虑以下类型:

public interface IRequest
{
    string Input { get; }
}
public interface IResponse
{
    string Output { get; }
}
public class SomeObject
{
    public string Value { get; set; }
}
abstract class BaseProfile<TSource, TDestination> : Profile
    where TSource : IRequest
    where TDestination : IResponse
{
    protected BaseProfile()
    {
        CreateMap<TSource, SomeObject>()
            .ForMember(src => src.Value, opt => opt.MapFrom(dest => dest.Input));
        CreateMap<SomeObject, TDestination>()
            .ForMember(src => src.Output, opt => opt.MapFrom(dest => dest.Value));
    }
}

我希望能够做到这一点,以便具体类型的配置文件可以使用此父配置文件中的映射,以便我能够定义它:

class FooRequest : IRequest
{
    public string Input { get; set; }
}
class FooResponse : IResponse
{
    public string Output { get; set; }
}
class FooProfile : BaseProfile<FooRequest, FooResponse>
{
    public FooProfile()
    {
    }
}

但是,到响应对象的映射根本不映射。

Mapper.Initialize(c => c.AddProfiles(typeof(FooProfile).Assembly));
var request = new FooRequest { Input = "FOO" };
var obj = Mapper.Map<SomeObject>(request); // this works
// obj.Value = "FOO"
var response = Mapper.Map<FooResponse>(obj); // this doesn't
// response.Output = null

似乎解决了这个问题,我必须复制我认为我已经创建但使用具体类型的映射。

public FooProfile()
{
    // Adding this works
    CreateMap<SomeObject, FooResponse>()
        .ForMember(src => src.Output, opt => opt.MapFrom(dest => dest.Value));
}

我是否缺少一些东西来完成这项工作?还是我每次都必须创建这些显式映射?


正如 Scott 的回答所指出的,问题最终是泛型参数受约束的接口没有可设置的属性。但纳德也发现使用ForMember() 方法的特定重载是可行的。因此,我们不是直接绑定到成员,而是绑定到成员名称。这可以满足我的需求。

protected BaseProfile()
{
    CreateMap<TSource, SomeObject>()
        .ForMember(src => src.Value, opt => opt.MapFrom(dest => dest.Input));
    CreateMap<SomeObject, TDestination>()
        .ForMember(nameof(IResponse.Output), opt => opt.MapFrom(dest => dest.Value));
}

【问题讨论】:

  • 它与超载一起工作!!!也许吉米以不止一种方式使用结果CreateMap(typeof(SomeObject), typeof(TDestination)) .ForMember("Output", opt =&gt; opt.MapFrom("Value"));
  • 嗯,你是对的,虽然唯一需要改变的是ForMember()调用的第一个参数。因此,我们不是绑定到IResponse.Output 成员,而是绑定到Output 名称。把它作为一个答案,这将是一个很好的解决方案。

标签: c# generics automapper


【解决方案1】:

为IResponse 的Output 属性添加一个setter。

public interface IResponse
{
    string Output { get; set; }
}

它正在尝试映射到 IReponse,但它无法设置该属性,因为它不可写。

也许这不是您想要的,因为您不希望 IResponse 具有可写属性。但这就是它没有设置好的原因。

在这种情况下,我可以确定的唯一解决方法是创建一个实现 IResponse 并具有可写属性的基本响应。

class ResponseBase : IResponse
{
    public string Output { get; set; }
}

class FooResponse : ResponseBase
{
}

然后将您的通用约束更改为基类而不是接口:

abstract class BaseProfile<TSource, TDestination> : Profile
    where TSource : IRequest
    where TDestination : ResponseBase

该通用约束将确定如何转换目标类型,进而确定该属性是否可写。

【讨论】:

  • 哦,就这么简单。对了,谢谢!我绝对希望将属性保留为只读,因此希望有不同的选择。
【解决方案2】:

我尝试了很多方法,但没有机会,但这种重载只适用于私人合同制定者

abstract class BaseProfile<TSource, TDestination> : Profile
        where TSource : IRequest
        where TDestination : IResponse
    {
        protected BaseProfile()
        {
            CreateMap<TSource, SomeObject>()
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Input));
            CreateMap(typeof(SomeObject), typeof(TDestination))
                .ForMember("Output", opt => opt.MapFrom("Value"));
        }
    }

也许吉米以不止一种方式使用结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2014-07-19
    相关资源
    最近更新 更多