gpt4 book ai didi

dependency-injection - CaSTLe - 使用构造函数注入(inject)类型工厂注入(inject)组件时,属性注入(inject)无法解析依赖关系

转载 作者:行者123 更新时间:2023-12-02 21:16:06 26 4
gpt4 key购买 nike

当使用构造函数注入(inject)工厂方法时,依赖的属性不会得到解析。但是,如果在解析依赖的组件之前解析了工厂方法,则一切都会按预期工作。此外,当仅使用属性注入(inject)或构造函数注入(inject)时,一切正常。请参阅下面的代码,显示工作场景和不工作场景(它使用 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);
}
}
}

最佳答案

您尚未注册Func<IFuncDep>在你的配置中。下面的代码应该可以工作:

_container.Register(Component.For<IFuncDep>().ImplementedBy<FuncDep>().LifeStyle.Transient);
_container.Register(Component.For<Func<IFuncDep>>().AsFactory().LifeStyle.Transient);

关于dependency-injection - CaSTLe - 使用构造函数注入(inject)类型工厂注入(inject)组件时,属性注入(inject)无法解析依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18481020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com