城堡温莎基于委托的工厂:传递参数的构造函数 [英] Castle Windsor delegate-based factory: passing parameters to the constructor

查看:115
本文介绍了城堡温莎基于委托的工厂:传递参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个 IConnectionFactory 接口,由 NMSConnectionFactory 类的构造看起来像这样实现的:

So, I have an IConnectionFactory interface, implemented by the NMSConnectionFactory class with a constructor that looks like this:

public NMSConnectionFactory(string providerURI, params object[] constructorParams)

我需要按需创建这些,和 providerURI 将只在运行时知道。 所以我想用它创建一个基于委托的工厂 IConnectionFactory 给我的(姑且称之为 connectionFactoryProvider

I need to create these on demand, and the providerURI will only be known at runtime. So I'm trying to use a delegate-based factory that creates IConnectionFactorys for me (let's call it connectionFactoryProvider).

看完城堡单证后,我已经试过这样: 我的另一个类的构造取决于这家工厂是这样的:

After reading the Castle documentations, I've tried this: My other class's constructor depends on this factory as such:

public ActiveMqSessionPool(Func<string, IConnectionFactory> connectionFactoryProvider)
{...}

这里面的构造,我用的是提供程序创建 IConnectionFactory 译文]这样的:

var connectionFactory = _connectionFactoryProvider("my destination");

然而,当我跑最后一行我得到一个错误说,NMSConnectionFactory正在等待这些依赖关系:

However, when I run this last line I get an error saying that the NMSConnectionFactory is waiting on these dependencies:

- Parameter 'providerURI' which was not provided. Did you forget to set the dependency?

- Service 'System.Object[]' which was not registered.

我如何通过 providerURI 的NMSConnectionFactory构造?

How can I pass the providerURI to the NMSConnectionFactory constructor?

这是我的组件注册的样子至今:

This is what my component registration looks like so far:

container.Register(Component.For<IConnectionFactory>()
                                    .ImplementedBy<NMSConnectionFactory>())
                 .Register(Component.For<ISessionPool>()
                                    .ImplementedBy<ActiveMqSessionPool>().LifeStyle.Transient)
                 .AddFacility<TypedFactoryFacility>();

我是否需要配置其他的东西?我缺少的是在这里吗?

Do I need to configure something else? What am I missing here?

推荐答案

您NMSConnectionFactory的构造函数的签名(字符串,对象[]),所以你的工厂方法需要有相同的,我认为。

Your NMSConnectionFactory's constructor has the signature (string, object[]), so your factory method needs to have the same I think.

尝试改变:

public ActiveMqSessionPool(Func<string, IConnectionFactory> connectionFactoryProvider)

public ActiveMqSessionPool(Func<string, object[], IConnectionFactory> connectionFactoryProvider

这篇关于城堡温莎基于委托的工厂:传递参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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