Remove components in Castle Windsor 3

倖福魔咒の 提交于 2019-12-10 16:58:12

问题


I am using the TypedFactoryFacility in Castle Windsor to allow me to use the interface-factory dependency injection.

I am having issues with the automatic delegate-factory injecting Func into automatically resolved components when these are not required (should be Null).

I would like to keep the TypedFactoryFacility, but remove the DelegateFactory, as per this question:

Can Windsor's TypedFactoryFacility's implicit delegate factory registration be disabled?

Unfortunately there is now no way to remove components from Castle Windsor (version 3).

Can someone suggest either a way to remove the DelegateFactory or somehow disable it so it does not inject Func into my services that can not be resolved (why is it even injecting Func that it does not know how to handle anyway??)


回答1:


I was unable to find how to remove a component (this should be added back in).

The best way I could find to disable the DelegateFactory was stop using the TypedFactoryFacility class to setup the Typed Factories and use the code from the Init function inside it, minus the delegate factory methods.

As so:

// Stop using AddFacility
//container.AddFacility<TypedFactoryFacility>();

// Initialise the TypedFactoryFacility manually, leaving out the DelegateFactory components.
container.Kernel.Register(new IRegistration[] {
   Component.For<TypedFactoryInterceptor>().NamedAutomatically(TypedFactoryFacility.InterceptorKey),
   // Disable DelegateFactory
   // Component.For<ILazyComponentLoader>().ImplementedBy<DelegateFactory>().NamedAutomatically(TypedFactoryFacility.DelegateFactoryKey),
   Component.For<ITypedFactoryComponentSelector>().ImplementedBy<DefaultTypedFactoryComponentSelector>().NamedAutomatically("Castle.TypedFactory.DefaultInterfaceFactoryComponentSelector"),
   // Disable DelegateFactory
   // Component.For<ITypedFactoryComponentSelector>().ImplementedBy<DefaultDelegateComponentSelector>().NamedAutomatically(TypedFactoryFacility.DefaultDelegateSelectorKey)
});
container.Kernel.ComponentModelBuilder.AddContributor(new TypedFactoryCachingInspector());

I had to use magic string "Castle.TypedFactory.DefaultInterfaceFactoryComponentSelector" DefaultInterfaceSelectorKey is an internal field.

Now you can use interface-factories, without delegate-factories messing everything up.



来源:https://stackoverflow.com/questions/19115475/remove-components-in-castle-windsor-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!