Solved

Should Models use EventAggregator in MVVM?

Posted on 2013-01-06
6
1,039 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

705 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