WPF, MVVM, ICommand, and repositories

I have a WPF application that looks something like this:

在这里输入图像描述

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?


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图

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.


The View Model should communicate to the Business Layer (Domain Objects + Domain Services) and not the repository directly. Even further, this communication should be done via Commands.

So you have:

View -> View Model -> Command -> Domain Object / Domain Service -> Repository

Unless you are developing a really simple CRUD application ...

链接地址: http://www.djcxy.com/p/56162.html

上一篇: 将命令绑定到子视图模型

下一篇: WPF,MVVM,ICommand和存储库