Question

In a GUI application, I am using an MVC with a Passive View, as described here.

This pattern is yet another variation on model-view-controller and model-view-presenter. As with these the UI is split between a view that handles display and a controller that responds to user gestures. The significant change with Passive View is that the view is made completely passive and is no longer responsible for updating itself from the model. As a result all of the view logic is in the controller. As a result, there is no dependencies in either direction between the view and the model.

So far, my Controller registers as a listener to existing, static components created by the Passive View itself at initialization. Now, the Controller needs to dynamically create a variable amount of UI components, depending on the Model (concretely, right now I am talking of a grid of checkboxes - the grid's dimensions is variable).

Here is where my hesitation lies:

  1. Should this dynamic UI creation code be implemented in the Controller? This would lead to less complex code resulting from keeping the View unaware of the Model, but a part of the presentation would be decided by the Controller...
  2. Should the View propose a generic, Model-independent way to create the UI components on demand, let the Controller use it and register listeners to the retrieved UI components? Here the Controller would have to convert back and forth between Model objects and generic objects (concretely, strings, integers, ...).
Was it helpful?

Solution

Whenever a view needs dynamic control creation, it tends to be for a collection of something. This means your Presenter/Controller does not need to create all of the logic, but call a method on the view which will create the controls.

On the View:

void PopulateUserOptions(IEnumerable<String> options)
{
    foreach (var item in options)
    {
        \\create and add your controls to the form
    }
}

This way the controller is expressing when a controll should be created etc, but leaving it to the view to decide how to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top