Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC for a desktop application, connection a view/controller pair with another one

I've had mostly experience with "server-side" mvc frameworks very popular in different languages like ASP.NET MVC or Zend Framework for php, Spring for Java etc.

Some of them are also possible to use for desktop applications development but I never tried that.

I fully understand that design patterns should not limit implementation, they should generally provide ideas and common rules that can be differently implemented.

Now I'm playing with one of those mvc frameworks for usual Desktop Applications development (it does not have many tutorials or a decent quickstart) and I have some questions regarding to the mvc paradigm. Here is one of them:

What are common ways to link different views / controllers? If I click a button, special controller for that button dispatches the event that is generated, does something with the model, changes view state. But what if I need to interact with another view? Like, when I click on a button, it changes a model, but also I need to open another window or change state of another window (hiding a button on another window let's say...), without changing actually the model. What are common ways here to address this? Should my first controller generate an event for the second controller (or second view)? Or should the second controller be handling events from first view?

Some links or examples for any languages/frameworks would be really helpful, thanks!

like image 753
lcf Avatar asked May 16 '10 01:05

lcf


People also ask

How model view and controller interact with each other?

First, the browser sends a request to the Controller. Then, the Controller interacts with the Model to send and receive data. The Controller then interacts with the View to render the data. The View is only concerned about how to present the information and not the final presentation.

What is MVC draw and explain MVC architecture for developing Web applications?

-MVC is an architectural pattern consisting of three parts: Model, View, Controller. Model: Handles data logic. View: It displays the information from the model to the user. Controller: It controls the data flow into a model object and updates the view whenever data changes.

What is MVC used for?

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display. This "separation of concerns" provides for a better division of labor and improved maintenance.

Can view directly interact with model in MVC?

A view queries the model in order to generate an appropriate user interface (for example the view lists the shopping cart's contents). The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself.


2 Answers

The common pattern for loosely coupled communication between objects is the Mediator pattern:

http://en.wikipedia.org/wiki/Mediator_pattern

This allows communication between objects through a mediator e.g. one object publishes a message to a mediator, and zero or more objects may subscribe\receive the message. The pub\sub is carried out through the mediator, not directly, so no direct coupling.

An example of this pattern is the "event aggregator service" in CAL (aka Prism), a composite application framework for WPF applications, typically used with an MVC-like pattern - MVVM:

http://msdn.microsoft.com/en-us/library/ff648465.aspx

http://msdn.microsoft.com/en-us/library/ff647600.aspx

Update

In MVVM, the event aggregator service (mediator) is typically used to allow loosely coupled communication between viewmodles (somewhat like controllers in MVC). In this way viewmodels can publish and subscribe to interesting general messages.

like image 185
Tim Lloyd Avatar answered Oct 02 '22 21:10

Tim Lloyd


Interesting question. My thoughts:

You're dealing with a fundamental difference between server-side and desktop: a Windows application can present more than one View at a time. (We'll ignore partial views and other hair-splitting for the moment.)

In MVC, the Controller is the where the Action happens (pun intended) so that's where things should be linked. You want a loosely-coupled solution, which calls for Dependency Injection. Think Spring, Unity, MEF, etc. This allows you to wire the Controllers together so that they can respond to events in each other without violating any principles (i.e. Separation of Concerns, Single Responsibility.)

like image 34
Dave Swersky Avatar answered Oct 02 '22 21:10

Dave Swersky