views:

134

answers:

1

When writing WinForms application with proper separation between View- and Controller/Presenter-functionality, I often find myself writing code like this:

public void class SomeView:Form{
  private void loadFileButton_Click(object sender, EventArgs e)
  {
    _loadFileAction.Execute(this);
  }
}

public void class LoadFileAction:Action{
  public override void Execute(IWin32Window owner){
    // ...
    string file = _fileSelector.SelectFile(owner);
  }
}

public void class FileSelector:IFileSelector{
  public override void SelectFile(IWin32Window owner){
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.ShowDialog(owner);
    //...
  }
}

or some other variation of passing around the correct window for opening sub-dialogs.

I am often tempted to just skip the window-handles and always use the parameter-less ShowDialog()-methods - which should just use the current active window.

Do anyone have any experiences using similar architectures? What do you do?

A: 

You could abstract the dialogs and put them behind a facade. That facade can then lookup the current active form and use that, or use nothing automatically. Then you don't need to pass it around.

Neil Barnwell