Question

Let's say I want to implement Passive View design pattern. I have a view which contains a listbox (which I perhaps will swap with listview or something else in the future). Now, with Passive View one should make the view as dumb as possible. Lets say I want to change the selection. I will put the logic for this in the presenter, thus I add a property to the views interface to get and set SelectedIndex property of the view's listbox. But if I in the future want to swap the listbox with a listview I am in trouble, because listview does not have a SelectedIndex property. Do I then implement some logic in the view (essentially making it a little less dumb) something like:

public int SelectedIndex
{
    get
    {
        if (myListView.SelectedIndices.Count > 0)
        {
            return myListView.SelectedIndices[0];
        }
        return -1;
    }
}

Or do I put some kind of adapter between the view and the presenter. What would be the most logical approach?

Was it helpful?

Solution

I think you need to get more abstract. A selected index could be considered too tightly coupled to a particular UI control. Something that, as you correctly point out, the pattern is trying to avoid so that views can be swapped seamlessly. Therefore, I would suggest the view has a property representing what is selected, be that a string or a more complex class, so that the particular view implementation can hide the actual translation from abstract to concrete. This way the presenter only ever deals with something meaningful to it, not the mechanics of a particular UI control.

OTHER TIPS

Yes you could do that. UI patterns such as Passive View, MVVM, MVC, MVP are general guidelines (they are not strict rules) on how to separate presentation from the application core logic to get a loosely coupled application that could be maintained and extended with minimum fuss. Now since since using a listview or listbox in your case is specific to the presentation you could put logic in the presentation to handle this without breaking this isolation between the UI and the application core.

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