I am trying to using IOC/DI container, but when come to creating a child window, what's the best practice?
Where I am having dilemma is :
public class ParentWindow : Form
{
public void OpenChildWindow()
{
var child = IocContainer.Instance.Resolve<ChildWindow>(); // big issue !!! an-ti server locator pattern
child.Show();
}
}
Or
public class ParentWindow : Form
{
private Container _container
public ParentWindow(Container container) // no, no, you have dependence on container
{
}
public void OpenChildWindow()
{
var child = _container.Resolve<ChildWindow>();
child.Show();
}
}
My solution
public class ParentWindow : Form
{
private IFormFactory _factory
public ParentWindow(IFormFactory factory) // inject from IOC container
{
}
public void OpenChildWindow()
{
var child = _factory.CreateChildWindow();
child.Show();
}
}
But with my solution, my factory kind of become my own IOC container, all my parent-ish window have to pass in a factory , isn't this make my factory become the new "server locator".
Is there any other better solution for this ?