c# - should I have this logic in a view model? -
c# - should I have this logic in a view model? -
i'm using asp.net mvc , not sure if logic have in view model appropriate. here sample model view:
class viewmodel { public string prop1 {get; set;} public string prop2 {get; set;} list<string> prop3 {get; set;} .......... // more properties public viewmodel() { prop1 = datamodel.field1 prop2 = datamodel.field2; prop3 = utilityclass.complexfunction(); ...... } }
the problem view model has lot of properties, , thought of initializing them in controller, seems cleaner encapsulate populating of properties within view model maintain controller leaner. if should doing in controller, rationale that? i'm thinking approach like:
viewmodel model = new viewmodel() {prop1 = datamodel.prop, prop3 = utilityclass.complexfunction()}
the controller should instantiate viewmodel object thinking:
viewmodel model = new viewmodel() {prop1 = datamodel.prop, prop3 = utilityclass.complexfunction()};
by doing so, viewmodel class no coupled class datamodel , utilityclass in other approach. viewmodel class can work in isolation without having needs know or depending on datamodel or utilityclass. helps viewmodel class beingness more testable.
c# asp.net asp.net-mvc
Comments
Post a Comment