2

我正试图让它工作 - 但我一定错过了一些东西 http://docs.castleproject.org/Windsor.Typed-Factory-Facility-delegate-based-factories.ashx#Registering_factories_implicitly_1

谁能发现它?

[TestClass]
public class UnitTest1 {

    [TestMethod]
    public void DelegateFactoryTest() {
        var container = new WindsorContainer();
        container.Register(
            Component.For<AComponent>().LifeStyle.Transient,
            Component.For<IAService>().ImplementedBy<AService>().LifeStyle.Transient
            );

        var comp = container.Resolve<AComponent>();
        Assert.IsNotNull(comp);
        Assert.IsNotNull(comp.GetService());
    }

    class AComponent {
        readonly Func<IAService> _serviceDelegate;

        public AComponent(Func<IAService> serviceDelegate) {
            _serviceDelegate = serviceDelegate;
        }

        public IAService GetService() {
            return _serviceDelegate();
        }
    }

    interface IAService { }

    class AService : IAService { }
}

得到以下错误

Castle.MicroKernel.Handlers.HandlerException:无法创建组件“Sandbox.Windsor.Tests.UnitTest1+AComponent”,因为它需要满足依赖关系。'Sandbox.Windsor.Tests.UnitTest1+AComponent' 正在等待以下依赖项: - 服务 'System.Func`1[[Sandbox.Windsor.Tests.UnitTest1+IAService, Sandbox.Windsor.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 未注册。

如果我明确注册,一切都很好

container.Register(
    Component.For<AComponent>().LifeStyle.Transient,
    Component.For<IAService>().ImplementedBy<AService>().LifeStyle.Transient,
    Component.For<Func<IAService>>().Instance(()=>new AService()).LifeStyle.Transient
);
4

1 回答 1

5

你不见了TypedFactoryFacility

container.AddFacility<TypedFactoryFacility>()
于 2012-04-18T01:18:20.227 回答