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

标签 dependency-injection castle-windsor

当使用构造函数注入(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/

相关文章:

c# - Windows Mobile 应用程序中的 IoC/DI 容器

grails - 从 Grails Holders 类中获取服务 bean

c# - 在 Windows 窗体应用程序中使用 CaSTLe Windsor

caSTLe-windsor - 温莎城堡 : Setting a parameter value to an empty string

c# - CaSTLe Windsor Ioc 容器不解析子依赖

nhibernate - 在共享主机上使用 CaSTLe Windsor 和 NHibernate 设施

javascript - Angular2 中没有服务提供者错误,为什么我需要将其注入(inject)到它的父组件中?

c# - 通过属性或 setter 方法的 ASP.NET Core MVC 依赖注入(inject)

c# - 在ctor中带参数的Windsor依赖注入(inject)

c# - 使用简单进样器根据需要加载装配体