开发者

Unity constructor parameters, with a twist

I have this type of class hierarchy:

public interface IA{}

public interface IB{void Foo();}

public class A:IA
{
 [Dependency]
 IB myb{get;set;}

 void myfunc(){ myb.Foo()}
}

public class B:IB
{
 public B(IA ia){}
}

I want to make an instance of class A with Unity. But when I want to make it I would want to write something like this:

public Testclass
{   
 void TestMethod()
 {
  IUnityContaine开发者_运维知识库r container = new UnityContainer();
  A currentA;
  container.RegisterType<IB,B>(new InjectionConstructor(currentA);
  currentA = container.Resolve<A>(); 
 }
}

This fails at the container.RegisterType line, saying that "Parameter type inference does not work for null values."

How to make this work?

Edit: Ahhhh how I hate this... Our Architect made two interfaces with the same name, and that's why I wasted 2 hours of my life. Daniel Hilgarth gave me a good idea, but I couldn't use it until I realised that resharper imported the wrong interface.


You have a circular dependency. That's bad. Either fix it or provide a factory for the creation of A.


In Ninject, you can defer some work by having your class implement IInitializable. You could do something similar here. In Initialize, you could fix up any dependencies that weren't possible during the constructor-based injection phase.

But, your question is not very clear. You state a problem, but not the specific question. Is it "why doesn't this work?", "should this work?", "can anyone suggest an alternative , strategy?", etc?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜