3

コンストラクターによって注入されたファクトリ メソッドを使用する場合、依存プロパティは解決されません。ただし、依存コンポーネントを解決する前にファクトリ メソッドが解決された場合、すべてが期待どおりに機能します。また、プロパティ注入またはコンストラクター注入のみを使用すると、すべてが機能します。動作するシナリオと動作しないシナリオを示す以下のコードを参照してください (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);
        }
    }
}
4

1 に答える 1