Displaying a form from a presenter in C #

I have one more architectural question. I am trying to implement MVP in C # as follows:

  • IView creates a presenter
  • IPresenter has an IView property that contains the View that is associated with it.
  • View CAN be Form, but Presenter does not distinguish between Form and non-Form types, so View can be swapped and the solution should be checked

What I sometimes need to do is open another form. For example, I have a browser view using a DataGrid, and when I double-click on a grid element or select something and click the "Edit" button, the "Edit" event appears and the "Lead" is active.

Now Presenter needs to open the editor's view, which is also a form, but the problem is that the moderator should not create the Form itself, because then it is impossible to execute the Mock View.

I'm pretty struggling with the right concept. My code looks something like this:

var editorView = new EditorForm(); editorView.Presenter.Entity = SelectedEntity; editorView.ShowDialog(View as Form); 

Under the hood, the EditorForm constructor creates a presenter and assigns this (view instance) to the presenter:

 public EditorForm() { Presenter = new EditorPresenter(this); InitializeComponents(); } 

In the perspective of the View, I can change it to MockView by simply executing Mock and then instantiating the same Presenter from the MockView constructor.

I searched for some other Q&A here and over the Internet, but did not find anything suitable.

Thank you for all your tips.

+4
source share
2 answers

If I understand your concept, I suggest you design the "Edit Presentation" task in accordance with the MVP template, as it was with the main view. Therefore, create IEditView and EditPresenter, and finally, in the main presenter, create an instance of EditPresenter. As a rule, control the editing presentation through its presenter.

0
source

After some brainstorming with some friends, we came to the conclusion that the best way to deal with the situation of creating an instance of various production view sets (FormViews) and another test set (MockViews) is to create them in some context - in my case, Spring context is an option.

So far, I have seen this as an answer to the problem. If you have an even smarter solution, please feel free to share!

0
source

All Articles