asp.net mvc 4 - c# given instance of base class, update base class properties of derived class -


i have view model (using mvc4) inherits base class

public class timetaskviewmodel : timedetailtask {     public string tasktypedescription { get; set; }  } 

i have method convert view model class, given instance of class. i'd able not have explicitly set every property of base class, update additional properties of view model:

public timetaskviewmodel convertclasstoviewmodel(timedetailtask entity) {     timetaskviewmodel viewmodel = new timetaskviewmodel();      viewmodel.base = entity;      viewmodel.tasktypedescription = entity.tasktypes.tasktypedescription;     return viewmodel; } 

anyway that? or way off base here?

note: need flat class result view model used in ienumerable format pass kendo ui grid on front end , can't handle complex classes.

it sounds you're looking easy way mapping information 1 class entirely different class happens have of same properties. don't think inheritance answer you're looking for. sounds want automapper, allow set rules mapping properties 1 object another.

for instance, if you're trying map view model database entity before writing database, you'd like:

var entity = mapper.map(viewmodel); 

for straight matches in property names, automapper take care of it, such instance copying timetaskviewmodel.tasktypedescription yourentity.tasktypedescription. in case name isn't perfect match or need manipulation of data (such casting string int), though, can set rules in map file.

in opinion, automapper great tool when you're using copy things class 1 property name class exact same property name. it's still okay tool when need copy same datatype between differently named properties, or perform simple conversions (such int string). starts feel more trouble when it's worth if doing complex conversions, though (mainly because find difficult debug , unit test mapping files), @ point feels should write own mapping function. of course, nothing says can't use simple cases , roll own mapping function more complex ones.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -