我只知道如何Register
和Resolve
新的实例。但是,我不确定如何将现有对象作为参数传递给我想要解析的实例。
Q1:
interface IPerson {
person FindAnOtherPerson();
void MakeFriends();
}
class Person : IPerson {
private List<Person> _people;
//...
void MakeFriends() {
var peter = FindAnOtherPerson(); //from _people
var rel = new Relationship(this, peter); //build a relationship
//how to pass params? something like Resolve<IRelationship>(this, peter)?
}
//...
}
Q2:
interface IParent { }
interface IChild { }
class Parent : IParent {
private IChild _child;
public Parent() {
_child = new Child(this); //parent knows child
//With DI, how do I resolve child and pass 'this' to child?
}
}
class Child : IChild {
private IParent _parent;
public Child(IParent p) { //child knows parent
_parent = p;
}
}
我想了一会儿,但没有足够的脑汁来解决它。请帮忙谢谢:)