wpf - How should a ViewModel interact with a Repository? -
i learning build application using mvvm, , have following situation:
- a viewmodel should display list of objects of type
examination
. list databoundlistview
in view, filtered, , user selectactiveexamination
further use. - a repository, "lives" in model layer, should "behave in-memory collection of domain objects", , instanced inside viewmodel.
now question is: should repository collection databound view, or should "datasource" property implementing inpc? example, following possibilities, 1 of them correct , other wrong, or both wrong...?
// example list replaced in order changed; // viewmodel class (part of it) public class thisviewmodel : viewmodelbase { public list<examination> examinationlist { { return _examinationlist; } set { _examinationlist = value; notifyofpropertychange("examinationlist"); } } } var repo = new examinationrepository(); thisviewmodel.examinationlist = repo.getall().where(ex => ex.value > 20).tolist();
second option
// example property repository // viewmodel class (part of it) public class thisviewmodel : viewmodelbase { // list repository in disguise. ienumerable _examinationlist = new examinationrepository(); public list<examination> examinationlist { { return _examinationlist; } set { _examinationlist = value; // should "notifyofcollectionchange", guess... notifyofpropertychange("examinationlist"); } } }
most quite confused/mistaken here, application rather small , simple architecture-wise, , don't plan use frameworks , advanced concepts have seen associated kind of problem (orm, ioc, di), instead concerned "how handle mutable, reppository-based collection in wpf/mvvm databinding environment".
edit: context application: application 1 performs clinical examinations. has list of patients, each patient has examinations. there patient repository , examinations repository. when select patient in patientlist, examinationslist of patient displays matching examinations examinations repo. user actions on patients , examinations crud, or bread (browse, read, edit, add , delete).
thanks advice!
in opinion, best separation in large applications, should always use separate collections fill database , display in ui. does mean you'll have iterate through collection twice, required in wpf anyway, add in display properties ui, such isselected
or isfocused
(if filling view model objects).
the main reason though allow interface our various layers in application can test each layer independently. if have no requirement this, maybe it's not important.
i've wondered why microsoft implement inotifypropertychanged
, inotifypropertychanging
interfaces in auto generated classes (from linq2sql instance) when using these classes in ui directly breaks separation between layers telling developers adhere to.
update >>>
i small application, doesn't matter how , these things. when writing small application, don't bother using mvvm. creating layers and/or implementing mvvm folder structure add additional development time , code , feel useful if you;
a) testing individual layers of application separately, or
b) might @ stage in future need 'swap out' 1 or more of layers, eg. move sql
database oracle
database instance, or move wpf ui web-based ui.
Comments
Post a Comment