Ninject - Binding a tree of data

六月ゝ 毕业季﹏ 提交于 2019-12-12 04:31:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!