问题
I had recently the opportunity to get started with Ninject and more generally with IOC. I came across a specific problem that is taking me quite some time to resolve.
I have a class A that contain childrens of the same class and a parent.
class ClassA : IClassA
{
ICollection<IClassA> childrens;
IClassA parent;
}
Specific instance of this class will be instanciated and I want to use Ninject to define my bindings, so that when calling for a specific instance of A, the right childrens and parent are populated. To make this happen I've created an XML file that will describe the relationship between parent and children. I just have then to parse the XML recursively and create my bindings.
My questions is now while building this Tree of bindings, how can I make childrens of an instance of ClassA persist. At some point I need to call a childrens.Add(kernel.Get("SpecificInstanceName")), but this does not persist when calling the parent with Ninject.
Example: I have an instance of ClassA called Main having two childrens lets call them FirstChildren and SecondChidren. I'll define my bindings with name. I'd like to when calling kernel.Get("Main") get the instance of ClassA "Main" with already its property childrens containing instances "FirstChildren" and "SecondChildren".
It's my first time posting, I hope I've made myself clear enough.
来源:https://stackoverflow.com/questions/43864334/ninject-binding-a-tree-of-data