Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Should Models use EventAggregator in MVVM?

Posted on 2013-01-06
6
Medium Priority
?
1,084 Views
Last Modified: 2013-02-01
In an MVVM architecture, the ViewModels use the EventAggregator to communicate between each other.

Should the Models use the EventAggregator to inform the ViewModels about any changes in the properties in the Model? If not, what should the Model use or do to inform the ViewModels about any changes in the data?
0
Comment
Question by:xenonn
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 38749172
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38758893
No. The ViewModel should have an instance of the model and can just call into it with accessors. The ViewModel is supposed to be the mediator between the View and the Model and should be able to communicate easily with both.
0
 

Author Comment

by:xenonn
ID: 38759104
@TommySzalapski: However, when a data changes in the Model class, it needs to dispatch an event to inform the ViewModel about the change. It isn't efficient to make the ViewModel to continously polling on the Model through calling the Model's accessors to check for changes in the data.

Hence, I am not sure if the Model should use the ViewModel's normal Event Aggregator, the Domain Event Aggregator, or the traditional C# delegate events to inform about its changes.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38760547
Are you using interrupts or do you have a listener thread in your ViewModel? I don't think the Model should need to alert the ViewModel of anything. When the user clicks on something in the View, it should request the information from the Model through the ViewModel. Then the ViewModel goes and gets it. The Model doesn't need to alert the ViewModel when something changes.
0
 

Author Comment

by:xenonn
ID: 38778613
If this were the case, how would the UI be informed if there is a change in the Model?

I am referring to a diagram here: http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-30-25-metablogapi/1207.mvvm_5F00_1299AEB4.png

In the diagram, I'm understanding it as the Model is implementing the INotifyProperty as a way to inform about its changes, isn't it? Or did I interpret it incorrectly?

I am not using interrupts or a listener thread in my ViewModel. I am implementing my own MVVM-like style for a game that I'm working on. I'm not using Prism or WPF, but just plain old C# code, except that I am designing and implementing the game with a MVVM design.
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 38787557
Yes. You are reading the diagram correctly. One issue I have with the diagram is he wrote "pure data" under Model which implies that it is passive. If it's sending change notifications, it is clearly more than just "pure data."

If you want to be able to have the Model tell the ViewModel things, you need some kind of way for the ViewModel to listen. You can use interrupts (not suggested) or a listener thread (push) or some kind of polling (pull).

Polling is when the ViewModel periodically checks with the Model to see if it changed. This may be what you want.

I still don't see why you need to notify the ViewModel when the Model changes though. The ViewModel should just know when it needs data and go get it.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Screencast - Getting to Know the Pipeline

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question