MVVM: Does the state of the model belong in the model or the viewmodel? -
i building application based on mvvm. have resolved of design issues, left conceptually crucial one: should viewmodel or model contain state of ever model models?
initially thought model should contain own state, turns out there lot of boilerplate involved when passing state viewmodel (which has 90% of model's state).
then though move entire state model viewmodel, doesn't sit quite right me, conceptually perceive viewmodel having state of view more state of model.
what accepted place put state?
you correct perceiving viewmodel having state of view. conceptually, state of model part of model, may varies depends on specific scenario. think of model data: can serialized, can come server and/or can/should persisted in database.
if example have shopping cart, items of shipping cart part of model. however, state in check-out process (payment method received, payment method verified, user confirmed) can go either way.
as boilerplate - paradigm works me reasonable amount of boiler plate containing (actually, referencing in c#) entire model within viewmodel, , exposing getters , setters properties in model, appropriate notification. e.g.
class personmodel { public string firstname { get; set; } public string lastname { get; set; } } class personviewmodel : inotifypropertychanged { private personmodel model; // next 4 lines can factored out baseviewmodel class private propertychangedeventhandler propertychanged; private raise(string propname) { this.propertychanged( new propertychangedeventargs(propname) ); } // ... repeat each property in model public string firstname { { return model.fistname; } set { model.firstname = value; raise('firstname'); } } public string lastname { { return model.lastname; } set { model.lastname = value; raise('lastname'); } } }
Comments
Post a Comment