IOC/DIコンテナを使用しようとしていますが、子ウィンドウを作成するときには何がベストプラクティスですか?IOC/DIコンテナと子ウィンドウ作成のジレンマ
私が午前ジレンマがある:
public class ParentWindow : Form
{
public void OpenChildWindow()
{
var child = IocContainer.Instance.Resolve<ChildWindow>(); // big issue !!! an-ti server locator pattern
child.Show();
}
}
それとも
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();
}
}
私のソリューション
public class ParentWindow : Form
{
private IFormFactory _factory
public ParentWindow(IFormFactory factory) // inject from IOC container
{
}
public void OpenChildWindow()
{
var child = _factory.CreateChildWindow();
child.Show();
}
}
しかし、私の解決策と 、一種の私自身のIOCのコンテナになる私の工場、すべての私の親の窓は工場で渡さなければなりません、これは私の工場を新しい "サーバロケータ"にするのではないのですか?
他にもこれは良い解決策がありますか?
関連:https://stackoverflow.com/questions/38417654/winforms-how-to-register-forms-with-ioc-container/38421425#38421425 – Steven
@Stevenは私の問題を解決しません – LeY