使用构造函数注入工厂方法时,不会解析依赖属性。但是,如果在解析依赖组件之前解析了工厂方法,则一切都按预期工作。此外,当仅使用属性注入或构造函数注入时,一切正常。请参阅下面的代码,显示工作和非工作方案(它使用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);
}
}
}
答案 0 :(得分:0)
您没有在配置中注册Func<IFuncDep>
。下面的代码应该有效:
_container.Register(Component.For<IFuncDep>().ImplementedBy<FuncDep>().LifeStyle.Transient);
_container.Register(Component.For<Func<IFuncDep>>().AsFactory().LifeStyle.Transient);