?
Solved

Should Models use EventAggregator in MVVM?

Posted on 2013-01-06
6
Medium Priority
?
1,057 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Six Sigma Control Plans
Starting up a Project
Suggested Courses

752 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