c# - Correct way to implement MVVM Design pattern when using SQLite -
so lets have sqlite database of person's
property name
public class person { private string _name; public string name { { homecoming _name; } set { _name = value; } } }
and have view listbox displaying names
<listbox itemsource={binding people}> <listbox.itemtemplate> <datatemplate> <label text="{binding name}"/> </datatemplate> </listbox.itemtemplate> </listbox>
and views datacontext peopleviewvm
public class peopleviewvm { public peopleviewvm { //do sqlite stuff, // ienumerable person's // create observable collection of people } private observablecollection<person> _people; public observablecollection<person> people { { homecoming _people; } set { _people = value; raisepropertychanged(); } } }
now understand simple example. unsure whether right implementation of mvvm design pattern. if person model means view binding straight model when binding property name
. if alter name of person in code behind won't reflected in view. right way illustration using mvvm design pattern?
that can "correct" implementation, based on requirements. wouldn't there's "correct", , "incorrect" issue. more like: improve scenario, or not?
people take bind models against view straight based on requirements, , how feel. simplify models, , wrap them "personviewmodel", in order expose more relevant properties, , not pollute model
.
if doesn't suit you, can download resharper(takes care of "trying" maintain view & viewmodel synchronized), or alternatively can encapsulate model further, , create "proxy" object, such:
public class personviewmodel { readonly person _person; public personviewmodel(person person) { _person = person; } public string name { { homecoming _person.name; } set { _person.name = value; raisepropertychanged(); } }
which seems pointless, helps maintain view , model more separate, in case of model entities alter often. resharper take care of cases, in experience.
c# sqlite design-patterns mvvm
No comments:
Post a Comment