c# - How to use the new IValueResolver of AutoMapper? -
i @ loss how use new ivalueresolver
interface in new version of automapper. perhaps used them improperly in previous versions of automapper...
i have lot of model classes, of them generated several databases on several database servers, using sqlmetal.
some of these classes has string property, publicationcode
, identifies publication subscription, or offer, or invoice, or whatever is, belongs to.
the publication can exist in either of 2 systems (the old , new system), hence have bool property on destination model classes tells whether publication in old or new system.
using old version (<5?) of automapper, used valueresolver<string, bool>
took publicationcode
input parameter, , returned bool
indicating location of publication (old or new system).
with new version (5+?) of automapper, seems no longer possible. new ivalueresolver requires unique implementation of each , every combination of source , destination models have, src.publicationcode
needs resolved dst.isinnewsystem
.
am trying use value resolvers in wrong way? there better way? main reason use resolver prefer have services injected constructor, , not having use dependencyresolver
, in code (i'm using autofac).
currently, use in following way:
// class linq-to-sql, non-related properties removed. public class findcustomerserviceselloffers { public string publicationcode { get; set; } }
this 1 of several data model classes have, contains publicationcode property). particular class mapped view model:
public class salespitchviewmodel { public bool isinnewsystem { get; set; } }
the mapping definition these 2 classes (where expression iprofileexpression), non-related mappings removed:
expression.createmap<findcustomerserviceselloffers, salespitchviewmodel>() .formember(d => d.isinnewsystem, o => o.resolveusing<publicationsystemresolver>().frommember(s => s.publicationcode));
and resolver:
public class publicationsystemresolver : valueresolver<string, bool> { private readonly publicationservice _publicationservice; public publicationsystemresolver(publicationservice publicationservice) { _publicationservice = publicationservice; } protected override bool resolvecore(string publicationcode) { return _publicationservice.isinnewsystem(publicationcode); } }
and use of mapper:
var result = context.findcustomerserviceselloffers.where(o => somecriteria).select(_mapper.map<salespitchviewmodel>).tolist();
you can create more general value resolver implementing imembervalueresolver<object, object, string, bool>
, using in mapping configuration. can provide source property resolution function before:
public class publicationsystemresolver : imembervalueresolver<object, object, string, bool> { private readonly publicationservice _publicationservice; public publicationsystemresolver(publicationservice publicationservice) { this._publicationservice = publicationservice; } public bool resolve(object source, object destination, string sourcemember, bool destmember, resolutioncontext context) { return _publicationservice.isinnewsystem(sourcemember); } } cfg.createmap<findcustomerserviceselloffers, salespitchviewmodel>() .formember(dest => dest.isinnewsystem, src => src.resolveusing<publicationsystemresolver, string>(s => s.publicationcode));
Comments
Post a Comment