c# - Why would an AutoMapperExtension behave differently from a direct implementation? -


i have defined following mapping within registermappings() of automapperconfig:

automapper.mapper.createmap<member, photoviewmodel>()             .formember(dest => dest.show, opt => opt.mapfrom(src => src.showphoto))             .formember(dest => dest.value, opt => opt.mapfrom(src => automapper.mapper.map<photovalueviewmodel>(src)); automapper.mapper.createmap<member, photovalueviewmodel>(); 

this mapping maps 2 properties of photoviewmodel. first maps show property based off of src.showphoto, , maps photovalueviewmodel value property.

the mapping works! unfortunately, have many objects require similar mapping. so, tried abstracting of implementation automapperextension method. method looks this:

public static imappingexpression<tsource, tdestination> applypropertymapping<tsource, tdestination>         (this imappingexpression<tsource, tdestination> imappingexpression, expression<func<tsource, bool?>> show, expression<func<tsource, object>> value)         tdestination : propertyviewmodel     {         imappingexpression             .formember(dest => dest.show, opt => opt.mapfrom(show))             .formember(dest => dest.value, opt => opt.mapfrom(value));         return imappingexpression;     } 

i'd expect extension method allow me define original mapping as:

automapper.mapper.createmap<member, photoviewmodel>()     .applypropertymapping(src => src.showphoto, src => automapper.mapper.map<photovalueviewmodel>(src)); 

but doesn't work! calls mapper.map<member, photoviewmodel>(memberobject, photoviewmodelobject) set photoviewmodelobject.value property null.

what causing difference?

the definitions of of these objects are:

public class member {     /**irrelevant properties not shown**/     public guid? photoid { get; set; }     public string photofilename { get; set; }     public bool? showphoto { get; set; } }  public class propertyviewmodel {     public object value { get; set; }     public bool? show { get; set; } }  public class photoviewmodel : propertyviewmodel {     public photovalueviewmodel value { get; set; } }  public class photovalueviewmodel {     public guid? photoid { get; set; }     public string photofilename { get; set; }     public string photourl {get {return utils.generatephotourl(photoid); } } } 

the photoviewmodel class contains 2 properties name value, 1 of them defined in propertyviewmodel , of type object. , other 1 defined in photoviewmodel , of type photovalueviewmodel (this hiding other property, should getting warning visual studio).

inside extension method referencing propertyviewmodel.value of type object

this because tdestination type declared in method assignable propertyviewmodel (via where tdestination : propertyviewmodel).

your extension method working correctly, changing value of property (propertyviewmodel.value).

your original mapping (without extension method) mapping value photoviewmodel.value of type photovalueviewmodel.

you can use visual studio debugger watch both propertyviewmodel.value , photoviewmodel.value in both cases (with or without extension method).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -