Solved

Should Models use EventAggregator in MVVM?

Posted on 2013-01-06
6
1,015 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 75
How useful is the free version of Selenium? 3 31
How to Convert a Quote to an Order in SalesForce 2 37
List<PaisEntity> - show some 1 24
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

786 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