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

一曲冷凌霜 提交于 2019-11-28 05:33:47

问题


We're using Windsor's typed factory facility and think it's lovely. We use the interface-based factories. However we'd like to disable some sub-sets of the delegate-based factories, specifically the implicitly registered factories. These are counter-intuitive, not because they are delegates, but because they are magically created, and can delay failure.

If we have a class which takes a delegate as a dependency

class X { public X(Func<int,IPrincipal> d); }

And then register it with a container which has had the typed factory facility registered.

container.Kernel.Register(Component.For<X>().ImplementedBy<X>())

I can resolve it without any trouble, which is counterintuitive at first, since no one told the container we had anything to say about Func<int,IPrincipal>.

var x = container.Resolve<X>();

And I don't run into failure until I try to actually use that implicitly created factory.

x.D(0); // no registration for IPrincipal

While this makes sense from a certain point of view, the fact that this factory is implicitly created is troublesome. It's a very container-aware behavior. People who write arbitrary classes will find it useful to parameterize their behavior via delegates, and as soon as we put them into the IoC container we run into this suprising behavior.

That said, there is one clever implicit factory that seems like it's worth keeping. Right now, Windsor will create a simple factory for dependencies of the form Func<T>, allowing the consumer of the dependency to delay actual creation. It might make sense in the 4.0 framework to change this to recognize Lazy<T> as a clear indication that you're just delaying construction of T, not attempting to access a factory which will implement an interesting policy.

Is there a clever switch available for configuring the TypedFactoryFacility or do we need to implement some new objects to get the behavior we want?


回答1:


If you really want to you can remove the DelegateFactory component from the container, which is what creates the delegate-based factories.



来源:https://stackoverflow.com/questions/5987323/can-windsors-typedfactoryfacilitys-implicit-delegate-factory-registration-be-d

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