相互に双方向接続するクラスがあります。NinjectがクラスParent
を作成すると、も作成されますChild
。問題は、Child
その親が誰であるかを知らなければならないということです。IContext
しかし、親オブジェクトに関する情報が見つかりません。
//The parent
class Parent:IParent
{
public Parent(Child child) {...}
}
//The child needing to know who its parent is
class Child:IChild
{
public Child(Parent parent) {...}
}
//The Ninject binding
Bind<IChild>().To<Child>.WithConstructorArgument("parent", x=>GetParent(x));
Bind<IParent>().To<Parent>;
Bind<IFactory>().ToFactory();
//Method to get the constructor parameter to Child containing the parent
private IParent GetParent(IContext context)
{
// Should return the IParent that requested this IChild
}
電話をかけるときIFactory.CreateParent()
、子と双方向に接続できる親を取得したいと思います。