Search code examples
c#listviewdesign-patternslistboxpassive-view

Passive View - view specific logic


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?


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.