views:

390

answers:

4

In .NET 3.5 winforms, I've created an MVC. I'd like to take the controller part and put it into a separate assembly, so that it has no knowledge of the view. My app uses five forms in its view. The controller takes care of initializing the forms at different times, which means the controller has a reference to the view (something I'd rather not). This means the controller can't be separated since it needs to know the form types being created, which probably means it isn't a true controller.

A: 

You can use Subject-Observer pattern between view and model to decouple the interaction between view and model. Further you can use MVP pattern.

Vinay
A: 

I don't want to couple the view and model or use MVP. I'd like to stay with MVC.

4thSpace
Either edit your original post, or use the comments. Don't post additional information in the comments
John Sheehan
A: 

I can declare Form variables in the controller. Nothing to do with the View. These are System.Windows.Forms. When the EXE is starting, it inits the controller (Program.cs). There, the specific Forms can be created and then assigned to the form properties on the controller. That allows the controller to be separated.

4thSpace
+2  A: 

I use an IOC Container to resolve the view and inject the controller instance via the form constructor, like so.

public class MainWindowController : WindowController<IMainWindowView>
{
}

public class WindowController<TView> where TView : IView
{
    public WindowController( IViewFactory factory ) {}

    public void ShowWindow() {}
    public void CloseWindow() {}
}

public interface IViewFactory
{
    IView CreateWithController( IWindowController controller ) {}
}

public interface IView {}
public interface IMainWindowView : IView {}

public class MainForm : Form, IMainWindowView
{
    public MainForm( IWindowController windowController )
    {
    }
}

My Window Controller class has a dependency on a view factory, which will create an instance of the registered View (using the container).

The ViewFactory uses the IOC container to create transient views and injects the controller instance into the constructor.

It also has methods like ShowWindow and CloseWindow that will show and hide the form. This allows me to unit test my controllers with mock views. It also removes any dependency on Windows Forms from my controller and model code, allowing me to replace any views with WPF views in the future.

I set it up that my main application logic, the view interfaces, the controllers and models live inside one main assembly and the other assembly only contains the Windows Forms forms and controls.

Shannon Cornish
Thanks. What is main application log? Is that your UI?
4thSpace
That would be main application logic (controllers, models, services etc) lives inside the main assembly and the other contains the windows forms controls.
Shannon Cornish