c# - Automapper: map properties manually -


i started use automapper map dtos<->entities , seems working great.

in special cases want map properties , perform additional checks. without automapper code looks (using fasterflect's propertyextensions):

object target; object source; string[] changedpropertynames = { };  foreach (var changedpropertyname in changedpropertynames) {     var newvalue = source.getpropertyvalue(changedpropertyname);     target.setpropertyvalue(changedpropertyname, newvalue); } 

of course code won't work if type conversions required. automapper uses built-in typeconverters , created specific typeconverter implementations.

now wonder whether possible map individual properties , use automapper's type conversion implementation, this

mapper.map(source, target, changedpropertyname); 

update

i think more information necessary:

i created maps, e.g.

mapper.createmap<calendarevent, calendareventform>() 

and created map custom typeconverter nullable datetime property in calendarevent, e.g.

mapper.createmap<datetimeoffset?, datetime?>().convertusing<nullabledatetimeoffsetconverter>(); 

i use these maps in web api odata controller. when posting new entitydtos, use

mapper.map(entitydto, entity); 

and save entity datastore.

but if using patch, delta<tdto> entitydto passed controller methods. therefore need call entitydto.getchangedpropertynames() , update existing persistent entity changed values.

basically working simple solution, if 1 of changed properties e.g. datetimeoffset? use nullabledatetimeoffsetconverter.

if want map select property have below

// create map var map = createmap<source,target>(); // ingnore existing binding of property map.forallmembers(opt => opt.ignore()); // map property following map.formember(dest => dest.prop1, opt => opt.mapfrom( src => src.prop1)); map.formember(dest => dest.prop2, opt => opt.mapfrom( src => src.prop2)); 

Comments