lundi 1 octobre 2018

Using MdiParent forms with MVP Design pattern in windows forms

I need to use a form as MdiParent and also use MVP pattern to implement it. Firstly I implement parent IPresenter interface as below:

public interface IPresenter<TView>
    {
        TView View { get; }
        void Run();
    }

Then I write parent IView interface as below:

public interface IView<TPresenter>
    {
        TPresenter Presenter { get; set; }
        void Run();
    }

Now I write the main form presenter as follow:

public interface IViewMainFormPresenter : IPresenter<IViewMainForm>
{
    void RunChildForm();
}

And the Main form presenter class is as below:

public class ViewMainFormPresenter : IViewMainFormPresenter
    {
        private readonly IViewMainForm _view;
        private readonly IApplicationController _appController;
        public ViewMainFormPresenter(IViewMainForm view, IApplicationController appController)
        {
            _view = view;
            _appController = appController;
            _view.Presenter = this;
        }
        public IViewMainForm View
        {
            get
            {
                return _view;
            }

        }

        public void Run()
        {

            _view.Run();
        }
}

The main form view interface:

public interface IViewMainForm : IView<IViewMainFormPresenter>
    {

    }

application controller interface:

public interface IApplicationController
{
   void RunChildForm();
}

The application Controller class:

public class ApplicationController : IApplicationController
{
public void RunChildForm()
        {
            using (var view = new ViewChildForm())
            {
                var presenter = new ViewChildFormPresenter(view, this);
                presenter.Run();
            }
        }
}

and the main form :

public partial class ViewMainForm : Form, IViewMainForm
    {
        private IViewMainFormPresenter _presenter;

        public ViewMainForm()
        {
            InitializeComponent();
        }
        public IViewMainFormPresenter Presenter
        {
            get
            {
                return _presenter;
            }
            set
            {
                if (_presenter == null)
                {
                    _presenter = value;
                }
            }
        }

        public void Run()
        {
            ShowDialog();
        }
}

and the same goes for ViewChild. but I dont know how can I set the main form as the mdiparent of child view

Aucun commentaire:

Enregistrer un commentaire