Lightweight WinForms MVP framework designed to provide separation guidance and testability but mostly get out of your way.
Create marker interface for your view:
public interface ISampleDialogView : IDialog
{
}
- Inheriting form IDialog allows the presenter to handle the grunt work of instansiating, binding, showing, and cleaning up your form. See here for more info.
Create SimpleDialogPresenter
inheriting from DialogPresenter
and a marker interface:
public class SampleDialogPresenter : DialogPresenter<ISampleDialogView>, IPresentSampleDialog
{
public SampleDialogPresenter(Func<ISampleDialogView> viewFactory)
: base(viewFactory)
{
}
}
public interface IPresentSampleDialog : IPresentDialog
{
}
- Note the use of generics:
DialogPresenter<ISimpleDialogView>
. By doing this we getISimpleDialogView
included for free in theView
base class variable. - The presenter constructors require view "factories" so that we do not need to incur a penalty for view instansiation until it's actually needed- that is, until it's ddisplayed to the user.
Create SimpleDialog
as a new form in your project:
public partial class SampleDialog : Dialog<IPresentSampleDialog>, ISampleDialogView
{
public SampleDialog()
{
InitializeComponent();
}
}
- Inheriting from
Dialog<IPresentSampleDialog>
gets us our presenter for free as the base class variablePresenter
inside the view. This allows us to do more complex view->presenter interaction, for example:
private void tsShowDialog_Click(object sender, EventArgs e)
{
Presenter.ShowDialogSample();
}
Seen here.
That's it. You're ready to go.
Create a dummy non-generic class to appease the designer.
public partial class MainForm : MainFormDesignable { }
//This gives the WinForms designer a default constructor which allows it to load the form
public class MainFormDesignable : View<IPresentMainForm> {}
Seen here