0

I want to build an application in C#, using the MVC pattern. I want first to create an instance of my controller and pass it a form as a parameter.

Here is the normal C# way (default):

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new mainForm(new Controller()));
}

The problem with this is that I don't want my mainForm class be able to access the Controller. I just want the controller to "see" the other objects.

I want something like this:

static void Main()
{
    Controller cnt = new Controller(new mainForm());
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(cnt);
}

This doesn't work though, because the method Run takes either a Form as an argument or an ApplicationContent. How can I do this?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
user1812076
  • 269
  • 5
  • 21
  • Is it WPF ? Check out MVVM frameworks like Caliburn.Micro. – thomasb Jun 19 '15 at 09:18
  • no, not really. I have the application already built, but it's not following the MVC pattern. I'm under time pressure, so... won't have time to learn new stuff atm, just want to find a solution to this, thanks though. – user1812076 Jun 19 '15 at 09:20
  • It's still not very clear if it's a WinForms or WPF application. I'm assuming WPF but I might be wrong. – thomasb Jun 19 '15 at 09:23
  • It's a WinForms application. – user1812076 Jun 19 '15 at 09:28
  • Yeah so use an `ApplicationContext` that has a `MainForm`. – CodeCaster Jun 19 '15 at 09:34
  • Why do you not want the main form to access the controller ? I'm not sure going "full mvc" with a "real" decoupling of objects would be easily done without either a heavy rewrite of your architecture, or using a preexisting framework. Check out this question : http://stackoverflow.com/questions/654722/implementing-mvc-with-windows-forms – thomasb Jun 19 '15 at 09:36
  • 3
    Deriving Controller from ApplicationContext is a simple and logical workaround. After all, it should be the controller that decides when the app should terminate. Do avoid reinventing this wheel, there are many MVC implementations around. – Hans Passant Jun 19 '15 at 09:37

0 Answers0