Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM: Provide a service to work with a specific part of the UI?

let's say that in my application, there is an user interface for representing a (geographic) map. It is integrated into the application as a UserControl and has its view model behind it.

Now, suppose I want to provide other parts of my application with a generic service interface to perform common tasks on the map (zoom, pan etc.) and not worry about the UI specifics. I could give away direct reference to the viewmodel, but I am pretty sure I would violate separation of concerns principle, not to mention it would be less testable.

So there are few questions:

  1. Does it make sense and is it good practice to implement such services (which act as an intermediate link to the UI) in the first place?
  2. Since the service operates directly on the map's viewmodel, should it be the viewmodel itself which implements the service interface?
  3. Is it appropriate for the service interface to provide events (e.g. besides providing a method to change the map scale, provide an event that the map scale was changed as well)? Or is it preferable to employ some kind of event broadcaster (aggregator) mechanism to push such notifications out of service interfaces?

Thanks in advance for your help.

like image 317
petr k. Avatar asked Dec 10 '11 15:12

petr k.


People also ask

What are the benefits of MVVM design pattern?

In Android, MVC refers to the default pattern where an Activity acts as a controller and XML files are views. MVVM treats both Activity classes and XML files as views, and ViewModel classes are where you write your business logic. It completely separates an app's UI from its logic.

What is the role of ViewModel in MVVM?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

What is a MVVM framework?

Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls. MVVM is also known as model-view-binder and was created by Microsoft architects Ken Cooper and John Gossman.


2 Answers

Consider using the Messenger in the MVVM Light toolkit. See more in another SO answer:

https://stackoverflow.com/a/2700324/117625

like image 129
Ed Chapel Avatar answered Nov 15 '22 07:11

Ed Chapel


EventAggregator is the another mechanism which establish communication between disconnected view models. I believe all the other parts of your application will be using the same MVVm and will have viewmodel to do the operations. Publish event say Zoom with required arguments from other parts of the app and catch it in the map using the subscribe mechanism.

http://msdn.microsoft.com/en-us/magazine/dd943055.aspx#id0420209

Prism has a good implementation of Event Aggregator. you can use that part.

like image 26
Joy George Kunjikkuru Avatar answered Nov 15 '22 07:11

Joy George Kunjikkuru