WinForms MVC with Dependency Injection
I am working on a project with a similar architecture.
I guess your main problem is that the event handlers of your view directly call the controller. E.g:
private void ribbonButtonTest_Click(object sender, EventArgs e)
{
controller.OpenNewSpreadsheet();
}
Try to avoid this. Let your controller objects be the masters of your application. Let the views and models be "blind and deaf".
When your view encounters a user action, just raise another event. Let the controller be responsible to register to this event and handle it. Your view is going to be like this:
public event EventHandler<EventArgs> RibbonButtonTestClicked ;
protected virtual void ribbonButtonTest_Click(object sender, EventArgs e)
{
var handler = RibbonButtonTestClicked;
if (handler != null) handler(this, EventArgs.Empty);
}
If you do this, you should be able to get rid of all the controller reference in the view. Your controller contstructor will look like this:
[Inject]
public ApplicationShellController(IApplicationShellView view)
{
this.shellView = view;
this.shellView.RibbonButtonTestClicked += this.RibbonButtonTestClicked;
}
Since you can not resolve your object tree from a view anymore, add a method "GetView()" to your controller and change your Program.Main() method:
CompositionRoot.Initialize(new DependencyModule());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var appCtrl = CompositionRoot.Resolve<ApplicationShellController>()
Application.Run(appCtrl.GetView());