Funq将现有对象作为参数传递

时间:2012-11-28 12:11:00

标签: c# dependency-injection servicestack funq

我只知道如何RegisterResolve新实例。但是,我不确定如何将现有对象作为参数传递给我想要解析的实例。

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; 
  }
}

我想了一段时间,但没有足够的脑汁来解决这个问题。请帮助谢谢:)

1 个答案:

答案 0 :(得分:1)

在我看来,你正试图在实体上使用依赖注入。这不是最常见的事情,因为与他们可能使用的长期服务相比,您可能会在系统中拥有许多人并且他们很短暂。这些服务(例如IFriendFinderIFriendMaker)将使用容器解析。

此处提供更多信息:Why not use an IoC container to resolve dependencies for entities/business objects?