views:

230

answers:

2

As I am further digging into MVVM and MVVM-light I recognized, that there is no MVVM-light provided base class for models.

But from my understanding, messaging and raising notifications could also happen in a model. At least in communication between models I would find that messaging would come in very handy.

So I just decided to derive my Model from ViewModelBase, even though some of the properties (like the design time ones) will be unused.

But the more I am looking at this, the more I think I have missed something. Is it considered "bad practice" to derive my Models from ViewModelBase?

And is it ok to use Messaging for Model communication?

+2  A: 

I would take a look at the EventAggregator in the Composite Application Library. The answer in this post has a good description of it. Jeremy Miller's post goes into a bit more detail.

SwDevMan81
Would that mean I have to abandon MVVM-light and go for Prism? I haven't decided a 100% yet for a MVVM framework, so the switch would be possible.
Marcus Riemer
Thats up to you, you can certainly download the code (Prism) and check it out. If you want to go some other route then you can just take the classes you need. IMO, I would use the Composite Application Library and use the MVVM model.
SwDevMan81
+8  A: 

Hi Marcus,

Derive your view-model classes from whatever you like... MVVM-light offers the VieWModelBase to provide an implementation of ICleanUp - which is good for managing the life-cycle of ViewModel objects. My own choice has been to implement all scaffolding for Property Change notifications in a base class, then derive from that for model classes. About the only strong suggestions I have regarding model classes are:

  1. Once size doesn't fit all. How you store your data may be different from how you interact with data, and ViewModel objects should be geared towards supporting interaction, not storage, and if you need to interact with the same data (Model) in two very different ways, then design two different ViewModels to support these different interactions.
  2. Do use attributes (a la System.ComponentModel) to annotate the models. You can get a lot of your validation work done this way - and validation feedback is the responsibility of the presentation layer (View + ViewModel), not the problem domain (i.e. the Model).

Really good ViewModel classes are also usually stateless enough that they can be recycled/reused within a single user interaction, such that large data lists can be virtualized (WPF supports virtualization) to save RAM.

Remember DRY (Do Not Repeat Yourself), KISS (Keep It Simple, Stupid!) and YAGNI (You ain't gonna need it) - are the principles you should keep in mind above any academic design principles. I've litereally wasted weeks on a WPF app implementing academically perfect MVC/MVVM patterns, only to find that they detracted form the overall understandability of the finished solution. So... keep it simple! :)

Mark
Although this is a very good answer, it does not quite meet my question. My considerations were mainly about model to model communication and best practises for models.
Marcus Riemer
"I've litereally wasted weeks on a WPF app implementing academically perfect MVC/MVVM patterns, only to find that they detracted form the overall understandability of the finished solution." This is exactly where I'm at with my MVVM learning curve -- this realization. I'm wondering, could you please elaborate a bit on point #2. Maybe an example of what you did and how it helped you?
bufferz