开发者

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, cha开发者_如何学运维nges 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!


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.


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜