开发者

What should go into the model and the view-model?

I'm developing a WPF Client which interacts with a WCF Web Service which persists data on a database using NHibernate.

The client will use the MVVM design pattern for the UI and inter开发者_JAVA百科acts with the WCF Service by sending and receiving DTOs.

From what I understand, only presentation data and logic should go in the View-Model, whereas the Model should contain the application's data and business logic.

Now let's use for example the case of a Login screen in the Client.

  • The View will represent the Login screen itself and its data context will be the View-Model.
  • The View-Model will hold an instance of the Model and will update the View by implementing the INotifyProperty interface.

Here's my question: what exactly should go in the Model ?

Should it hold a reference to an interface of a WCF proxy using a dependency injection container such as MEF ?

Basically, the user would type in a username and password, which would update the properties in the view-model due to the databinding. Whenever the user presses the "Login" button on the view, a command is sent to the View-Model which in turn forwards it to the Model. The model then uses the Proxy interface to communicate with the WCF Web Service.

Is this approach correct ? If not, what exactly should go in the Model and View-Model?


The ViewModel in MVVM is a combination of the ApplicationModel and Controller in classic MVC. As such it is responsible for connecting to services to query for data to display and invoke operations against the service.

The workflow should be something like this:

  1. LoginViewModel is created and gets a handle to the WCF service (ideally, you should use DI to inject the WCF service as an ILoginService)
  2. User enters username and password which are bound to corresponding properties on the VM and clicks login which is bound to a delegate command on the VM.
  3. Within the command handler the VM runs validation (e.g. make sure that username and password have been set)
  4. The VM invokes LoginUser on the ILoginService passing the username and password (invoking the operation on the WCF service.

The model shouldn't be aware of services and the like. Technically, you are using pure DTOs on the client side so it should have minimal logic, behind the service, there should be a richer model that does have business logic and validation.


If you check Karls Shiffletts blog you'll find a good diagram about the MVVM-Layers.
I would say your implementation looks okay to me.

It depends on you implementation, most propably you will switch the ViewModel and have a DataTemplate (the View) for the ViewModel. This CodeProject link shows a good implementation: Creating an Internationalized Wizard in WPF


Models should not contain data access or any business logic beyond validating their data. They are data objects, and should contain nothing more than data and validation.

Your ViewModels are in charge of running your application, and this includes filling the data objects (Models), altering them, saving/deleting them, and providing them (and other related properties) to the View.

I have a simple MVVM example here if you're interested. There is a definition at the very top that defines what should go into the Model compared to the ViewModel

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜