Humble View / MVP with WinForms and UserControls Collection

I am refinancing a WinForms (.NET 4) application that uses TabControl to host a UserControl - a UserControl instance is created in each tab with the final result being an editor on each tab. They edit a collection of elements that are ultimately fed into an object edited by the form as a whole.

As an example of a class structure:

  • class School
    • string Name
    • string Address
    • A collection of Course s, each with several corresponding fields ( Department , Name , etc.)

(This is not a school-related application, but the metaphor works.)

Visually, the UserControls set is controlled by Course es, and the parent form processes School information.

Right now I have a presenter for the form / school and a presenter for the UserControl / Course with a presentation for everyone. However, the school teacher must monitor some information for the Courses. For example, options selected for one course limit options in others. The School model handles the calculations, but it needs to get to the leading course.

I don't have much success finding examples of this type of relationship in MVP discussions, and this is my first approach to MVP. What are some good options for this? Is a collection of course instructors suitable for the school teacher to present the recruitment? Should a school see a collection of Courses views? (The final UserControls should be attached to the form somehow and somewhere, right?)

My main goals (unsurprisingly) increase testing and maintainability, and the main sources in this process were Michael Fairs "Dialog with Haml" and Jeremy Miller "Build You Own CAB".

+4
source share
1 answer

As I deal with a similar situation, the parent leader should know about the presenters of the child (as constructor dependencies).

Each child presenter has its own view, so in the parent presenter, my logic looks something like this:

Initialize () - initialize the parent - call initialization on each child presenter (this is to get all the necessary data. EXCLUDE the data that is mainly displayed. For example, if you have a leading invoice, you need to get a collection from the client, if you have there will be a client combo box so that you can change this for the invoice) - insert child views into the parent view (the parent element is usually a form where the children are user controls).

then after that, usually when loading a parent, using some LoadXXX method, I also load the children. In your example, it will be something like

schoolPresenter.LoadSchool (school)

which, in turn, downloads data to all speakers, for example, loads parent controls with information about the school, passes a collection of courses to the cursor, etc ...

You might think that I noticed that it’s good to do this, have a Refresh () method for each of these speakers, which basically knows how to load itself based on the current state. Perhaps you may not have such a method on the parent presenter, but simple presenters work just fine, so this means that in the LoadSchool method you can have something like

coursesPresenter.Courses = school.Courses; coursesPresenter.Refresh ();

+2
source

Source: https://habr.com/ru/post/1394029/


All Articles