Solved

Should Models use EventAggregator in MVVM?

Posted on 2013-01-06
6
993 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
Comment Utility
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
Comment Utility
@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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Scripting vs. Programming languages 25 110
XML to SQL Table using c# 5 44
How do I bind the results to a grid 3 22
Get String split 5 31
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now