Reputation: 16246
I only know how to Register
and Resolve
new instances. However, I am not sure how to pass existing objects as parameters in to an instance I want to 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;
}
}
I thought about this for awhile but not enough brain juice to work it out. Please help thanks :)
Upvotes: 1
Views: 239
Reputation: 172676
It seems to me that you are trying to use dependency injection on entities. This is not the most usual thing to do, since you will probably have many persons in the system and they are short lived, compared to long-lived services that they might use. Those services (such as IFriendFinder
and IFriendMaker
) would be resolved using a container.
More information here: Why not use an IoC container to resolve dependencies for entities/business objects?
Upvotes: 1