c# - Automapper multiple memberOptions -
i have viewmodel needs data 2 collections. 2 collections members of indicatiestelling
. map pass instance of indicatiestelling
.
each property uses valueresolver gets right value out of given collection. make work need register valueresolver each property , source each property. tried this:
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); });
this code doesn't work, still mapping errors:
missing type map configuration or unsupported mapping.
mapping types: hashset`1 -> boolean
i searched example/docs using multiple member options, nothing came up. supported? , ifso, doing wrong here?
i don't think can combined in way want, little refactoring can use this:
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)));
(i refactored ivalueresolver
s static
methods)
or can make ivalueresolver
s know member at, e.g. hardcoded:
public class indicatiestellingfactvalueresolver : ivalueresolver { public resolutionresult resolve(resolutionresult source) { var model = (model.indicatiestelling)source.value; var obj = model.indicatiestellingfacts; // calculate obj } } mapper.createmap<model.indicatiestelling, clientrechtmatigheiddto>() .formember(dest => dest.hasfactbeoordelenrechtmatigheid, opt => opt.resolveusing<indicatiestellingfactvalueresolver<model.indicatiestelling>>()) // etc
or using 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 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
Comments
Post a Comment