依赖的属性在使用构造函数注入的工厂方法时不会得到解析。但是,如果在解析依赖的组件之前解析了工厂方法,那么一切都会按预期工作。此外,当仅使用属性注入或构造函数注入时,一切正常。请参阅下面显示工作和不工作场景的代码(它使用 Microsoft 单元测试框架)。
这是一个不受支持的场景(并且有原因)还是一个错误?
using System;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CastleTest
{
public interface IFuncDep
{}
internal class FuncDep : IFuncDep
{}
internal class UsingFuncDepPropInjected
{
public Func<IFuncDep> FuncDepProp { get; set; }
}
internal class UsingFuncDepConsInjected
{
public Func<IFuncDep> FuncDepProp { get; private set; }
public UsingFuncDepConsInjected(Func<IFuncDep> funcDepProp)
{
FuncDepProp = funcDepProp;
}
}
internal class PropInjectedUsingConsInjected
{
public UsingFuncDepConsInjected FuncDep { get; set; }
}
internal class PropInjectedUsingPropInjected
{
public UsingFuncDepPropInjected FuncDep { get; set; }
}
internal class ConsInjectedUsingPropInjected
{
public UsingFuncDepPropInjected FuncDep { get; private set; }
public ConsInjectedUsingPropInjected(UsingFuncDepPropInjected funcDep)
{
FuncDep = funcDep;
}
}
internal class ConsInjectedUsingConsInjected
{
public UsingFuncDepConsInjected FuncDep { get; private set; }
public ConsInjectedUsingConsInjected(UsingFuncDepConsInjected funcDep)
{
FuncDep = funcDep;
}
}
[TestClass]
public class CastleTest
{
private WindsorContainer _container;
[TestInitialize]
public void InitContainer()
{
_container = new WindsorContainer();
_container.AddFacility<TypedFactoryFacility>();
_container.Register(
Component.For<IFuncDep>().UsingFactoryMethod((k, c) => new FuncDep()).LifeStyle.Transient);
//_container.Register(Component.For<IFuncDep>().ImplementedBy<FuncDep>().LifeStyle.Transient);
_container.Register(Component.For<UsingFuncDepPropInjected>());
_container.Register(Component.For<UsingFuncDepConsInjected>());
_container.Register(Component.For<PropInjectedUsingConsInjected>());
_container.Register(Component.For<PropInjectedUsingPropInjected>());
_container.Register(Component.For<ConsInjectedUsingPropInjected>());
_container.Register(Component.For<ConsInjectedUsingConsInjected>());
var handlers = _container.Kernel.GetAssignableHandlers(typeof(object));
foreach (var handler in handlers)
{
foreach (var serviceType in handler.ComponentModel.Services)
{
Console.Write(serviceType.Name);
}
Console.WriteLine(": {0}", handler.ComponentModel.Implementation.FullName);
}
}
[TestMethod]
public void ConstructorInjectionWithinPropertyInjection_Failing()
{
//var func = container.Resolve<Func<IFuncDep>>();
//Assert.IsNotNull(func);
//IFuncDep value = func();
//Assert.IsInstanceOfType(value, typeof(FuncDep));
//when the block above is uncommented, everything gets working (see ConstructorInjectionWithinPropertyInjection_ManualFuncResolveBeforePropResolve_Ok)
var o = _container.Resolve<PropInjectedUsingConsInjected>();
Assert.IsNotNull(o);
Assert.IsNotNull(o.FuncDep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[TestMethod]
public void PropertyInjectionWithinPropertyInjection_Ok()
{
var o = _container.Resolve<PropInjectedUsingPropInjected>();
Assert.IsNotNull(o);
Assert.IsNotNull(o.FuncDep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[TestMethod]
public void ConstructorInjectionWithinPropertyInjection_ManualFuncResolveBeforePropResolve_Ok()
{
var func = _container.Resolve<Func<IFuncDep>>();
Assert.IsNotNull(func);
IFuncDep value = func();
Assert.IsInstanceOfType(value, typeof(FuncDep));
var o = _container.Resolve<PropInjectedUsingConsInjected>();
Assert.IsNotNull(o);
Assert.IsNotNull(o.FuncDep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[TestMethod]
public void PropertyInjectionWithinConstructorInjection_Ok()
{
var o = _container.Resolve<ConsInjectedUsingPropInjected>();
Assert.IsNotNull(o);
Assert.IsNotNull(o.FuncDep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[TestMethod]
public void ConstructorInjectionWithinConstructorInjection_Ok()
{
var o = _container.Resolve<ConsInjectedUsingConsInjected>();
Assert.IsNotNull(o);
Assert.IsNotNull(o.FuncDep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
}
}