Coupling between controller and view

Posted by cheez on Stack Overflow See other posts from Stack Overflow or by cheez
Published on 2010-04-07T00:56:24Z Indexed on 2010/04/07 1:03 UTC
Read the original article Hit count: 439

Filed under:
|
|
|
|

The litmus test for me for a good MVC implementation is how easy it is to swap out the view. I've always done this really badly due to being lazy but now I want to do it right. This is in C++ but it should apply equally to non-desktop applications, if I am to believe the hype.

Here is one example: the application controller has to check some URL for existence in the background. It may connect to the "URL available" event (using Boost Signals) as follows:

BackgroundUrlCheckerThread(Controller & controller)
{
   // ...
   signalUrlAvailable.connect(
      boost::bind(&Controller::urlAvailable,&controller,_1))
}

So what does Controller::urlAvailable look like?

Here is one possibility:

void
Controller::urlAvailable(Url url)
{
    if(!view->askUser("URL available, wanna download it?"))
      return;
    else
      // Download the url in a new thread, repeat
}

This, to me, seems like a gross coupling of the view and the controller. Such a coupling makes it impossible to implement the view when using the web (coroutines aside.)

Another possibility:

void
Controller::urlAvailable(Url url)
{
   urlAvailableSignal(url); // Now, any view interested can do what it wants
}

I'm partial to the latter but it appears that if I do this there will be:

  1. 40 billion such signals. The application controller can get huge for a non-trivial application
  2. A very real possibility that a given view accidentally ignores some signals (APIs can inform you at link-time, but signals/slots are run-time)

Thanks in advance.

© Stack Overflow or respective owner

Related posts about mvc

Related posts about coupling