c# - Domain to ViewModel Design? -
i wondering best design following. have domain models like:
public class car { public string name { get; set; } //other common properties removed brevity }
in domain model have models fuel specific cars inherit car like:
public class dieselcar : car { public bool runonbiodiesel { get; set; } //other diesel specific properties removed brevity } public class petrolcar : car { public int nosparkplugs { get; set; } //other petrol specific properties removed brevity }
on view have common car view model. common properties used , depending on fuel type render partial view either diesel or petrol - again partial view takes complete carview model there common properties on each partial view well.
however rather map multiple properties view model domain in controller , domain view model in view model builder wanting take adavantage of automapper this.
however think mean having below in automapperbootstrapper (and i'm not sure work correctly??)
mapper.createmap<domain.car, carviewmodel>(); mapper.createmap<domain.dieselcar, carviewmodel>(); mapper.createmap<domain.petrolcar, carviewmodel>(); mapper.createmap<carviewmodel, domain.car>(); mapper.createmap<carviewmodel, domain.dieselcar>(); mapper.createmap<carviewmodel, domain.petrolcar>();
i thinking other way in view model have 3 different classes commoncarfields, dieselcarfields , petrolcarfields , in carviewmodel constructor new each 1 of classes , map each domain model corresponding class on viewmodel , vice versa in automapper?
perhaps can suggest better way of doing missing?
i surprised if needed every property in domain model in view. tend flatten data when sending view, or sending over-the-wire. i've found in both cases, if you're keeping identical object graphs in both domain , view models, religiously mapping between two, you're doing wrong somewhere.
whenever i've built application-centric app, direct mapping never happens naturally. in data-centric apps, on other hand, see structure of database being (incorrectly) forced through every layer.
Comments
Post a Comment