Windsor的TypedFactoryFacility隐式委托工厂注册可以被禁用吗? [英] Can Windsor's TypedFactoryFacility's implicit delegate factory registration be disabled?

查看:149
本文介绍了Windsor的TypedFactoryFacility隐式委托工厂注册可以被禁用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Windsor的打字工厂设施认为它很可爱。我们使用基于界面的工厂。不过,我们希望禁用的一些子集基于代表的工厂,特别是隐含注册的工厂。这些是反直觉的,不是因为它们是代表,而是因为它们是神奇创建的,并且可能会延迟失败。

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>())

我可以解决它没有任何麻烦,这是开始违反直觉,因为没有人告诉容器我们有什么要说关于 Func< int,IPrincipal>

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>();

我不会遇到失败,直到我尝试使用这个隐式创建的工厂。 p>

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

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

虽然这在某种意义上是有道理的,工厂是隐藏的创建是很麻烦的。这是一个非常容易识别的行为。编写任意类的人会发现通过代表来参数化他们的行为是有用的,一旦我们将它们放入到IoC容器中,我们遇到这种惊奇的行为。

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.

说,有一个聪明的隐性工厂,似乎值得保留。现在,Windsor将为 Func< T> 形式的依赖关系创建一个简单的工厂,允许消费者依赖关系延迟实际创建。在4.0框架中可能会有所改变,以识别 Lazy< T> ,这表明您只是延迟构建 T ,不尝试访问将实施有趣的策略的工厂。

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.

是否有一个聪明的开关可用于配置 TypedFactoryFacility 或者我们需要实现一些新对象来获取我们想要的行为吗?

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?

推荐答案

如果您真的想要从容器中删除 DelegateFactory 组件,这就是创建基于委托的工厂。

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

这篇关于Windsor的TypedFactoryFacility隐式委托工厂注册可以被禁用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆