Dependency injection Castle-在使用构造函数注入的类型化工厂注入组件时,属性注入不会解决依赖关系
依赖属性在使用构造函数注入的工厂方法时无法解析。但是,如果在解析依赖组件之前解析工厂方法,则一切都会按预期工作。另外,当使用独占属性注入或构造函数注入时,一切都正常。请参阅下面显示工作和不工作场景的代码(它使用Microsoft单元测试框架) 这是一个不受支持的场景(有什么原因)还是一个bugDependency injection Castle-在使用构造函数注入的类型化工厂注入组件时,属性注入不会解决依赖关系,dependency-injection,castle-windsor,Dependency Injection,Castle Windsor,依赖属性在使用构造函数注入的工厂方法时无法解析。但是,如果在解析依赖组件之前解析工厂方法,则一切都会按预期工作。另外,当使用独占属性注入或构造函数注入时,一切都正常。请参阅下面显示工作和不工作场景的代码(它使用Microsoft单元测试框架) 这是一个不受支持的场景(有什么原因)还是一个bug using System; using Castle.Facilities.TypedFactory; using Castle.MicroKernel.Registration; using Castl
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);
}
}
}
使用系统;
使用Castle.Facilities.TypedFactory;
使用Castle.MicroKernel.Registration;
使用温莎城堡;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
名称空间CastleTest
{
公共接口IFuncDep
{}
内部类FuncDep:IFuncDep
{}
使用FunctionDepProjected的内部类
{
公共函数FuncDepProp{get;set;}
}
使用FunctionDepConsinjected的内部类
{
public Func FuncDepProp{get;private set;}
公共使用FuncDepConsinjected(Func funcDepProp)
{
FuncDepProp=FuncDepProp;
}
}
内部类ProjecteductionConsinjected
{
public UsingFuncDepConsInjected FuncDep{get;set;}
}
内部类ProjectedUsingProjected
{
public UsingFuncDepProjected FuncDep{get;set;}
}
内部类ConsinjectedusingProjected
{
public UsingFuncDepProjected FuncDep{get;private set;}
公共考虑(使用FuncDepProjected funcDep)
{
FuncDep=FuncDep;
}
}
内部类一致性教育一致性
{
public UsingFuncDepConsInjected FuncDep{get;private set;}
公共协商教育协商(使用FuncDepConsinjected funcDep)
{
FuncDep=FuncDep;
}
}
[测试类]
公营卡斯特莱特酒店
{
私人温莎集装箱;
[测试初始化]
公共容器()
{
_容器=新WindsorContainer();
_container.AddFacility();
_集装箱。登记(
Component.For().UsingFactoryMethod((k,c)=>new FuncDep()).lifesture.Transient);
//_container.Register(Component.For().ImplementedBy().lifety.Transient);
_container.Register(Component.For());
_container.Register(Component.For());
_container.Register(Component.For());
_container.Register(Component.For());
_container.Register(Component.For());
_container.Register(Component.For());
var handlers=_container.Kernel.GetAssignableHandlers(typeof(object));
foreach(处理程序中的变量处理程序)
{
foreach(handler.ComponentModel.Services中的var serviceType)
{
Console.Write(serviceType.Name);
}
WriteLine(“:{0}”,handler.ComponentModel.Implementation.FullName);
}
}
[测试方法]
public void构造函数injection with inproperty\u Failing()
{
//var func=container.Resolve();
//Assert.IsNotNull(func);
//IFuncDep value=func();
//Assert.IsInstanceOfType(value,typeof(FuncDep));
//当上面的块未注释时,一切正常(请参阅ConstructorInjjectionWithInPropertyInjection\u ManualFuncResolveBeforePropResolve\u Ok)
var o=_container.Resolve();
Assert.IsNotNull(o);
Assert.IsNotNull(o.fundep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[测试方法]
public void property injection with inproperty injection_Ok()
{
var o=_container.Resolve();
Assert.IsNotNull(o);
Assert.IsNotNull(o.fundep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[测试方法]
public void构造函数InProperty Injection的引用\u ManualFuncResolveBeforePropResolve\u Ok()
{
var func=_container.Resolve();
Assert.IsNotNull(func);
IFuncDep value=func();
Assert.IsInstanceOfType(value,typeof(FuncDep));
var o=_container.Resolve();
Assert.IsNotNull(o);
Assert.IsNotNull(o.fundep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[测试方法]
公共无效属性injection with inconstructorinjection_Ok()
{
var o=_container.Resolve();
Assert.IsNotNull(o);
Assert.IsNotNull(o.fundep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
[测试方法]
public void constructorinjunction with inconstructorinjunction_Ok()
{
var o=_container.Resolve();
Assert.IsNotNull(o);
Assert.IsNotNull(o.fundep);
Assert.IsNotNull(o.FuncDep.FuncDepProp);
}
}
}
您没有在配置中注册Func
。下面的代码应该有效:
_container.Register(Component.For<IFuncDep>().ImplementedBy<FuncDep>().LifeStyle.Transient);
_container.Register(Component.For<Func<IFuncDep>>().AsFactory().LifeStyle.Transient);
\u container.Register(Component.For().ImplementedBy().lifesture.Transient);
_container.Register(Component.For().AsFactory().LifeStyle.Transient);
谢谢您的回答。提议的解决方案有效。然而,我想了解为什么其他案例在没有明确注册Func
为工厂的情况下运行良好。由于\u container.AddFacility(),工厂应自动解析代码>(在所有标记为“Ok”的情况下,它都会得到解决)。