MVP in WinForms ( presenter & program.cs)

519 views Asked by At

I'm learning an MVP pattern and have the following issues:

If I have one form(project MainForm), one model(project Model) and one presenter, should I create a new project for my presenter or it's ok to put it in MainForm project?

1)If presenter must be located in separate project, it obviously needs a reference to MainForm for it's constructor, what leads us to the second problem:

When the app starts in program.cs (which is in MainForm) I need to create my presenter:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Presenter presenter = new Presenter(new MainForm()); //Here it is
        Presenter.Run();
    }

But, since presenter is in a separate project, I can not use it without a reference. However, there is already a reference from presenter to MainForm so i can't add it.

2)If presenter is located in MainForm project, the program starts fine, but to use functions from Model I have to add a reference from MainForm project to Model project, which, I think, contradicts MVP pattern.

Please, tell me how to design my sulution properly.

1

There are 1 answers

0
Peter Duniho On BEST ANSWER

Your View and Model should be independent of each other and the Presenter. They may live in their own projects, but it would be fine in a simple program for everything to be in the same project.

If you do want them in separate projects, that is easily done. The main project, containing the Program class, will also contain the Presenter class. The View (i.e. the MainForm class) and the Model would (could) then be in their own projects.

The hierarchy of projects would look something like this:

  • Main program EXE project: contains Presenter class and Program class (i.e. the latter for the program entry point, Main() as you show in your code example). References include:
    • View DLL project: contains MainForm class (and any other view classes)
    • Model DLL project: contains model class(es)

In other words, you can create a Windows Forms project for the main program project, and just delete the default Form1 that's created there for you. You can create a class library project for the view DLL project and add a Windows Form class to that project for your MainForm class.