たとえば、次のように、再帰的にネストされたものを作成する必要がある状況を考えてみましょう。
public interface IRecurrentTestNodeFactory
{
RecurrentTestNode Create(int num);
}
public class RecurrentTestNode
{
public int Num { get; set; }
public RecurrentTestNode Child { get; set; }
public RecurrentTestNode(int num, IRecurrentTestNodeFactory factory)
{
Num = num;
Child = num > 0 ? factory.Create(num - 1) : null;
}
}
明らかな実装は次のようになります。
public class ManualRecurrentTestNodeFactory : IRecurrentTestNodeFactory
{
public RecurrentTestNode Create(int num)
{
return new RecurrentTestNode(num, this);
}
}
[Test]
public void ManualRecurrentTest()
{
var root = new ManualRecurrentTestNodeFactory().Create(1);
Assert.NotNull(root);
Assert.AreEqual(1, root.Num);
Assert.NotNull(root.Child);
Assert.AreEqual(0, root.Child.Num);
Assert.Null(root.Child.Child);
}
このテストはパスします。しかし、Windsor の Typed Factory Facility で同じことをしようとすると、次のようになります。
[Test]
public void RecurrentTest()
{
var windsor = new WindsorContainer();
windsor.Kernel.AddFacility<TypedFactoryFacility>();
windsor.Register(Component.For<IRecurrentTestNodeFactory>().AsFactory());
windsor.Register(Component.For<RecurrentTestNode>().LifeStyle.Transient);
var f = windsor.Resolve<IRecurrentTestNodeFactory>();
var root = f.Create(1);
Assert.NotNull(root);
Assert.AreEqual(1, root.Num);
Assert.NotNull(root.Child);
Assert.AreEqual(0, root.Child.Num);
Assert.Null(root.Child.Child);
}
この例外で失敗しています:
Castle.MicroKernel.ComponentActivator.ComponentActivatorException : ComponentActivator: could not instantiate Tests.RecurrentTestNode
----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> Castle.MicroKernel.CircularDependencyException : Dependency cycle has been detected when trying to resolve component 'Tests.RecurrentTestNode'.
The resolution tree that resulted in the cycle is the following:
Component 'Tests.RecurrentTestNode' resolved as dependency of
component 'Tests.RecurrentTestNode' which is the root component being resolved.
サービスの場合にそのようなコードが失敗する理由は明らかですが、ファクトリの場合は不必要に制限されているようです。単純な int の代わりに、コンテナーで解決された依存関係がたくさんあるため、ファクトリ バリアントのままにしたいと思います。