Richard
Richard

Reputation: 14625

MVVM design question

I'm trying to create an silverlight application using the MVVM design pattern. It's a kind of bank application.

I've watched a lot of tutorials on MVVM but something makes me real confused.

I have about fiwe usercontrols representing my views "TransactionsView", "AccountView" etc and a bunch of models "UserProfile" - containing user password, username and a list of UserAccounts, "UserAccounts" - containing name, balance and a list of AccountTransactions, "AccountTransactions" - containing a name, and ammount.

Should i create one modelview which contains my userprofile or should i create a viewmodel for every view i have? I'm a doing right so far? Or have i got it completley wrong?

Thanks

Upvotes: 2

Views: 258

Answers (5)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59101

In MVVM, ViewModels are usually 1-to-1 with Views. There isn't a parity between number of ViewModel and Models, though.

  • View: UI
  • ViewModel: Handles changes to view state, forwarding them to the model if/when appropriate. Sends notifications from the underlying program back to the user. It may also do initial UI validation.
  • Model: Actual "guts" of the application. Algorithms, data storage, system calls etc go here. I put program flow here. I've seen other people put it in the ViewModel. That part is up to you to figure out.

A View always needs a ViewModel, hence 1-to-1 (it could have sub-models, but I'll leave that up to you to decide on/deal with. I'd start off with 1-to-1).

A ViewModel usually needs Models to actually "do work", but how many classes/instances is up to each app/problem you're trying to solve.

Upvotes: 2

Jimmy Chandra
Jimmy Chandra

Reputation: 6580

Imagine what you want to see on the screen and each state the screen / controls on the screen might be in, everything that is needed on that particular screen (view) should have a corresponding property in your ViewModel that you can bind the View to. So, this translate to a single ViewModel for a particular View. The ViewModel itself can be tied into one or more model(s) in the back. At least that's how I understand it.

Upvotes: 0

James Hay
James Hay

Reputation: 12700

There's no real hard and fast rules but essentially there's normally one ViewModel per View. You can get into a situation where you want to share a view model across multiple views but it's rare.

Upvotes: 0

ShahidAzim
ShahidAzim

Reputation: 1494

One ViewModel per view is recommended for MVVM.

Upvotes: 0

Tedd Hansen
Tedd Hansen

Reputation: 12362

From what you explain you are going in the right direction. What viewmodel you create is a bit up to you, MVVM is not set in stone - its just a method. What I found through trial and ERROR was that it was smart to understand it well before digging myself in too deep.

I read many articles that didn't explain MVVM in a way I could understand. Finally I found a couple of articles by Jeremiah Morrill that were straight to the point and easy to understand: Article 1 and article 2.

Upvotes: 0

Related Questions