The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Should a view instantiate it's own controller

Usually I write code like this

Dim aView as new View
Dim aController as new ViewController(aView)

However sometimes I forget to instantiate a controller. This mostly happens because most views that I use are controls that I place within a form and are automatically constructed.

If a view were to create a controller in it's constructor:
Public sub New()
Me.Controller = new ViewController(me)
End sub

All my problems would be solved, since every view would always have a controller. Does this have any downsides?
Praveen Angyan Send private email
Sunday, June 24, 2007
I think it might be cleaner to have the controller instantiate a view, instead.

This way, if you need multiple views, you aren't ending up (by default) with multiple controllers. You just create as many views as needed for each controller. If you need multiple contollers, create them explicitly, each with a default view.
Sunday, June 24, 2007
I wresteled with this when I first saw Composite UI Application Block (CAB). They let the view instantiate the controller. My take on it is that there is no golden rule (duh! ;-) but I look at it from a code reuse perspective. If you have a scenario where you could reuse the same controller for different implementations of a view (interface) than the view should perhaps instantiate its own controller. If you have different controllers for the same (type of) views than the controller should be passed to the view. It also depends on how strict the seperation is between the controller and view classes.

I usually start with the view creating its own controller and when I find that I want to switch controllers for a specific view (implementation) I refactor the code.
Marc Jacobi Send private email
Monday, June 25, 2007
I prefer to have the view instantiate the controller.  It seems to be a more natural way from a unit testing perspective.  You can develop the controller before you even have a real implementation of the view.  If the controller does the 'new View(...)' then your controller's tests are dependent on the View implementation, most likely.
Patrick Lioi
Monday, June 25, 2007
In my humble opinion it depends who knows about who. In my case (Excel VBA application) it's the controller (MyApp class) who knows how to do tasks and the view (frmApp form) who knows where to find the controller.

This allows the controller to be used by other UIs than then form - e.g. there's a button in my Outlook taskbar, which uses one function of that particular Excel application from within Outlook window.

Since it seems that they might be more than one view to a single controller it seems reasonable  that like in a 1 to n relation the multiple instance (views) side knows about the singular one. Thus, the view controls and instantiates the controller.
Wojtek Send private email
Wednesday, June 27, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz