IoC and MVP

Oct 27, 2015 at 8:48 PM
I'm working on a Compact Framework application that uses the IoC library and follows the Model-View-Presenter pattern. I do this by adding the view (SmartPart) to the SmartParts collection, then manually instantiating the presenter by passing an instance of the view. Any service dependencies are passed in via service dependency properties.
AdminView view = RootWorkItem.SmartParts.GetOrCreate<AdminView>("view:Admin");
AdminPresenter presenter = RootWorkItem.Items.Get<AdminPresenter>("presenter:Admin");
if (presenter == null)
{
    presenter = new AdminPresenter(view);
    RootWorkItem.Items.Add(presenter, "presenter:Admin");
}
workspace.Show(view);
I would prefer to have the IoC container pass in service dependencies via an injection constructor, but I'm not sure how to pass in the view. It doesn't feel right to create the view in the SmartParts collection and then add it to the Services collection. Is there a better way?
Coordinator
Oct 28, 2015 at 10:41 PM
I guess the answer is "it depends." It's really an architecture call. Typically I have a single instance of each presenter type, and the View gets it's presenter at construction:
    class MyView
    {
        private MyPresenter m_presenter;
        public MyView()
        {
            m_presenter = RootWorkItem.Services.GetOrCreate<MyPresenter>();
        }
    }
Then if you just do this:
var view = workspace.Show<MyView>();
Everything just gets built up and registered.