Click here to Skip to main content
15,886,919 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: TIL Pin
CDP180216-Feb-17 2:16
CDP180216-Feb-17 2:16 
GeneralRe: TIL Pin
Sander Rossel16-Feb-17 3:57
professionalSander Rossel16-Feb-17 3:57 
GeneralRe: TIL Pin
CDP180216-Feb-17 4:04
CDP180216-Feb-17 4:04 
GeneralMVVM Pin
Bernhard Hiller1-Feb-17 4:21
Bernhard Hiller1-Feb-17 4:21 
GeneralRe: MVVM Pin
Pete O'Hanlon1-Feb-17 5:57
mvePete O'Hanlon1-Feb-17 5:57 
GeneralRe: MVVM Pin
kdmote2-Feb-17 5:09
kdmote2-Feb-17 5:09 
GeneralRe: MVVM Pin
Gerry Schmitz2-Feb-17 6:12
mveGerry Schmitz2-Feb-17 6:12 
GeneralRe: MVVM Pin
Pete O'Hanlon2-Feb-17 6:25
mvePete O'Hanlon2-Feb-17 6:25 
Okay, the thing it sounds like you're struggling with here is what the concept of the ViewModel is in MVVM. The VM is, basically, the glue between the view and the models and allows you to "shape" the interactions from the views. Part of the reason to separate it is that you can more easily change what the View looks like and how it interacts with the models. By models, I'm talking about anything that isn't a view object - this could be writing to a log file, saving data to a database, reading files from the disk, and so on. Now, the biggest advantage of separating out the VM is, as you noted, Separation of Concerns, but this is best coupled with unit testing. In other words, having a ViewModel makes it a lot easier to test the behaviour of logical areas of code in isolation - typically, you'd have your models injected into your VM as interfaces which could easily be mocked. This is a lot harder to do in code behind - it's a whole lot harder because IoC isn't directly supported in creating windows and user controls.

Okay, that's fairly high level but here's one of my favourite advantages with the VM approach. There's an MVVM concept called ViewModel first - this talks about how you want to compose a particular view onto the UI. I shall apologise in advance for this because this is going to assume a lot of knowledge, but I shall try to simplify it where possible. With VM first, you put a plain vanilla Control onto your UI then you populate the template of this based off a binding. What this means is that you say that the control accepts a Binding of a type. Then, at runtime, we populate this with a ViewModel - this population might be driven by choosing to show a particular document type off a menu (for instance), and each document type is a different ViewModel. At this point, through the magic that is WPF, it looks for an appropriate template to display in the control. If we haven't associated a particular template (it's a DataTemplate I'm talking about here) with the ViewModel, it looks back through the inheritance chain of the ViewModel until it finds a matching template - the default one you'll see just displays the name of the class. If we have created a DataTemplate that matches the ViewModel, that particular template will be rendered in the control.

So, ultimately, the ViewModel approach allows us to decouple what the View looks like, from the behaviour of the control.

I hope that helps (and as a shameless plug, I have recently started a series of articles that aims to demystify MVVM and explain the whys and wherefores of it without any of the misconceptions that exist around it, you can find the articles here[^] and here[^])
This space for rent

GeneralRe: MVVM Pin
kdmote2-Feb-17 7:46
kdmote2-Feb-17 7:46 
GeneralRe: MVVM Pin
Dominic Burford14-Feb-17 2:58
professionalDominic Burford14-Feb-17 2:58 
GeneralRe: MVVM Pin
Pete O'Hanlon14-Feb-17 4:29
mvePete O'Hanlon14-Feb-17 4:29 
GeneralRe: MVVM Pin
Dominic Burford14-Feb-17 5:47
professionalDominic Burford14-Feb-17 5:47 
GeneralRe: MVVM PinPopular
Clifford Nelson1-Feb-17 6:59
Clifford Nelson1-Feb-17 6:59 
GeneralRe: MVVM Pin
Bernhard Hiller1-Feb-17 21:09
Bernhard Hiller1-Feb-17 21:09 
GeneralRe: MVVM Pin
Sander Rossel1-Feb-17 12:53
professionalSander Rossel1-Feb-17 12:53 
GeneralRe: MVVM Pin
johannesnestler1-Feb-17 22:11
johannesnestler1-Feb-17 22:11 
GeneralRe: MVVM PinPopular
mbb011-Feb-17 23:18
mbb011-Feb-17 23:18 
GeneralRe: MVVM Pin
Member 113540342-Feb-17 12:06
Member 113540342-Feb-17 12:06 
GeneralRe: MVVM Pin
Brady Kelly2-Feb-17 2:55
Brady Kelly2-Feb-17 2:55 
GeneralRe: MVVM Pin
Gerry Schmitz2-Feb-17 5:57
mveGerry Schmitz2-Feb-17 5:57 
GeneralRe: MVVM Pin
Arlo Weston3-Feb-17 0:49
Arlo Weston3-Feb-17 0:49 
GeneralRe: MVVM Pin
Dennis_E13-Feb-17 22:37
professionalDennis_E13-Feb-17 22:37 
GeneralRe: MVVM Pin
Nelek14-Feb-17 7:23
protectorNelek14-Feb-17 7:23 
GeneralRe: MVVM Pin
YaakovF23-Feb-17 2:50
YaakovF23-Feb-17 2:50 
Generalnew bool() Pin
Marc Clifton31-Jan-17 7:01
mvaMarc Clifton31-Jan-17 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.