I have components:
public interface IFoo
{ }
public interface IBar
{ }
public class Foo : IFoo
{
public IBar Bar { get; set; }
}
public class Bar : IBar
{
public IFoo Foo { get; set; }
}
I have a Castle-Windsor configuration:
Container.AddComponent("IFoo", typeof (IFoo), typeof (Foo));
Container.AddComponent("IBar", typeof (IBar), typeof (Bar));
and unit test fails:
[Test]
public void FooBarTest()
{
var container = ObjFactory.Container;
var foo = container.Resolve<IFoo>();
var bar = container.Resolve<IBar>();
Assert.IsNotNull(((Foo) foo).Bar);
Assert.IsNotNull(((Bar) bar).Foo);
}
It does not work due to the circular dependency, "foo" .Bar or "bar" .Foo is null. How to configure the lock to properly initialize both components?
I can correctly initialize both components manually:
[Test]
public void FooBarTManualest()
{
var fooObj = new Foo();
var barObj = new Bar();
fooObj.Bar = barObj;
barObj.Foo = fooObj;
var foo = (IFoo) fooObj;
var bar = (IBar) barObj;
Assert.IsNotNull(((Foo)foo).Bar);
Assert.IsNotNull(((Bar)bar).Foo);
}
.. and he works, passes. How to make such a configuration using Castle Windsor?
source
share