2

我有一个需要来自 2 个集合的数据的 ViewModel。这 2 个集合是Indicatiestelling. 所以为了映射这个,我传递了一个Indicatiestelling.

每个属性都使用一个 ValueResolver 从给定集合中获取正确的值。为了完成这项工作,我需要为每个属性注册 ValueResolver 以及每个属性的源。我试图这样做:

Mapper.CreateMap<Model.Indicatiestelling, ClientRechtmatigheidDto>()
                .ForMember(dest => dest.HasFactBeoordelenRechtmatigheid, (opt) => { opt.ResolveUsing<IndicatiestellingFactValueResolver>(); opt.MapFrom(src => src.IndicatiestellingFacts); })
                .ForMember(dest => dest.HasFactRechtmatig, (opt) => { opt.ResolveUsing<IndicatiestellingFactValueResolver>(); opt.MapFrom(src => src.IndicatiestellingFacts); })
                .ForMember(dest => dest.SoortVoorziening, (opt) => { opt.ResolveUsing<IndicatiestellingAnswerValueResolver>(); opt.MapFrom(src => src.IndicatiestellingAnswer); })
                .ForMember(dest => dest.ZZP, (opt) => { opt.ResolveUsing<IndicatiestellingAnswerValueResolver>(); opt.MapFrom(src => src.IndicatiestellingAnswer); });

此代码不起作用,我仍然遇到映射错误:

缺少类型映射配置或不支持的映射。

映射类型:HashSet`1 -> Boolean

我搜索了有关使用多个成员选项的示例/文档,但没有出现任何结果。是否支持?如果是这样,我在这里做错了什么?

4

1 回答 1

8

我认为它们不能以您想要的方式组合,但是通过一些重构您可以使用它:

Mapper.CreateMap<Model.Indicatiestelling, ClientRechtmatigheidDto>()
    .ForMember(dest => dest.HasFactBeoordelenRechtmatigheid, opt => opt.ResolveUsing(src => IndicatiestellingFactValueResolver.Resolve(src.IndicatiestellingFacts)))
    .ForMember(dest => dest.HasFactRechtmatig, opt => opt.ResolveUsing(src => IndicatiestellingFactValueResolver.Resolve(src.IndicatiestellingFacts)))
    .ForMember(dest => dest.SoortVoorziening, opt => opt.ResolveUsing(src => IndicatiestellingAnswerValueResolver.Resolve(src.IndicatiestellingAnswer)))
    .ForMember(dest => dest.ZZP, opt => opt.ResolveUsing(src => IndicatiestellingAnswerValueResolver.Resolve(src.IndicatiestellingAnswer)));

IValueResolver(我将您的 s重构为static方法)

或者您可以让您IValueResolver的 s 知道要查看哪个成员,例如硬编码:

public class IndicatiestellingFactValueResolver : IValueResolver
{
    public ResolutionResult Resolve(ResolutionResult source)
    {
        var model = (Model.Indicatiestelling)source.Value;
        var obj = model.IndicatiestellingFacts;
        // calculate with obj
    }
}

Mapper.CreateMap<Model.Indicatiestelling, ClientRechtmatigheidDto>()
    .ForMember(dest => dest.HasFactBeoordelenRechtmatigheid, opt => opt.ResolveUsing<IndicatiestellingFactValueResolver<Model.Indicatiestelling>>())
    // etc

或使用Func

public class IndicatiestellingFactValueResolver<TSource> : IValueResolver
{
    private Func<TSource, object> selector;
    public IndicatiestellingFactValueResolver(Func<TSource, object> selector)
    {
        this.selector = selector;
    }
    public ResolutionResult Resolve(ResolutionResult source)
    {
        var model = (TSource)source.Value;
        object obj = selector(model);
        // calculate with obj
    }
}

Mapper.CreateMap<Model.Indicatiestelling, ClientRechtmatigheidDto>()
    .ForMember(dest => dest.HasFactBeoordelenRechtmatigheid,
    opt => opt.ResolveUsing<IndicatiestellingFactValueResolver<Model.Indicatiestelling>>()
                .ConstructedBy(() => new IndicatiestellingFactValueResolver<Model.Indicatiestelling>(x => x.IndicatiestellingFacts)))
    // etc
于 2013-10-07T14:01:15.970 回答