Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF, MVVM, ICommand, and repositories

I have a WPF application that looks something like this:

enter image description here

The viewmodel wraps the model and exposes any attributes relevant to the view via INotifyChanged. The view is also bound to several ICommand objects that handle certain behavior triggered by the view. I have an external ICommand who's sole purpose is to save the model to a database.

Everything I've read indicates that neither the view or the viewmodel should have a reference to the repository. This is the reason for Command 3 which is outside of the viewmodel.

My question is twofold. First, is this a reasonable architecture, and second, what is a good way to get the model instance over to command 3 so it can be put in the repository?

like image 999
ConditionRacer Avatar asked Dec 13 '12 19:12

ConditionRacer


1 Answers

I, personally, see no problem with having the ViewModel have a reference to the repository. Trying to avoid this will cause unnecessary complications.

In MVVM, the ViewModel is typically the "glue" layer that sits above your Model - and the Repository is part of the Model (it's part of the domain specific data/logic). My blog series on MVVM shows a good image of how I personally think about this:

MVVM Diagram

Letting the VM work with the Repository directly by putting Command 3 into the VM would likely be cleaner than trying to separate it out.

like image 94
Reed Copsey Avatar answered Oct 22 '22 18:10

Reed Copsey