从结合中停止Ninject Func <T,T,bool>自动 [英] Stop Ninject from binding Func&lt;T, T, bool&gt; automatically

查看:142
本文介绍了从结合中停止Ninject Func <T,T,bool>自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PriorityQueue,它需要一个Func键施工参数。

I have a PriorityQueue that takes a Func as construction parameter.

public PriorityQueue(ISomeOtherInjectedThing other, Func<T, T, bool> cmp_func) {...}

我必将这使用Ninject:

I bound this using Ninject:

Bind(typeof (IPriorityQueue<,>)).To(typeof(PriorityQueue<,>));

我们必须在代码中,并作为这部分一个奇怪的错误,我们注意到Ninject似乎产生对象FUNC键,注入到我们的优先级队列这一点,但我们没有为这个绑定。工厂应该抛出的异常activtion,因为我们并没有传递所需的比较FUNC。它没有,如果我调试,我觉得这是:

We had a weird bug in code and as part of this, we noticed that Ninject seems to generate an object Func and injects this into our priority queue, but we don't have a binding for this. The factory should have thrown an activtion exception, since we didn't pass the required comparison func. It didn't, and if I debug, I find this:

Ninject.dll!Ninject.KernelBase.Resolve.AnonymousMethod__c(Ninject.Planning.Bindings.IBinding binding) Line 386 + 0x5a bytes C#

我怎样才能获得Ninject到抛出一个异常,如预期,而不是默默地生成函数求<>注入?

How can I get Ninject to throw an exception as expected instead of silently generating a Func<> to inject?

推荐答案

该函数功能 - 自动绑定到Func键-厂是Ninject.Extensions.Factory的一个特点。见来源:
FuncModule.cs

The Func-Auto binding to Func-Factory is a feature of Ninject.Extensions.Factory. See source: FuncModule.cs

如果您创建一个特定的结合Func键< T,T> 它会仍然覆盖由 FuncModule 通用绑定创建。但是当你注意到了,不会有任何例外,如果你不创建一个特定的绑定。

If you create a specific binding for Func<T, T> it will still override the generic binding created by FuncModule. But as you noticed, there won't be any exceptions if you don't create that specific binding.

最简单的方法来摆脱默认的(开放式)的结合为摆脱工厂扩展。

The simplest way to get rid of the default (open) binding is getting rid of the Factory extension.

但是,这可能是一个有点激烈。你也可以做什么,而不是被禁用自动加载扩展

But that is probably a bit drastic. What you can also do instead, is disabling Auto-Extension loading:

var kernel = new StandardKernel(new NinjectSettings {LoadExtensions = false});



那么你就必须动态地加载扩展 - 这是通过加载它们的 NinjectModule 实现。例如:

IKernel.Load<FuncModule>();



当然的 FuncModule 你不要加载,我们正在做这一切摆脱它。但是,你必须这样做,所有你真正想要的其他扩展模块。
最后你必须创建一个你需要的所有工厂扩展绑定:

Of course the FuncModule you don't want to load, we're doing all this to get rid of it. But you'll have to do it for all the other extension modules you actually want. Finally you'll have to create all Factory extension bindings that you need:

if (!this.Kernel.GetBindings(typeof(Func<IContext, IResolutionRoot>)).Any())
{
    this.Bind<Func<IContext, IResolutionRoot>>().ToMethod(ctx => context => context.Kernel);
}

this.Bind<FuncProvider>().ToSelf().InSingletonScope();
this.Bind<IFunctionFactory>().To<FunctionFactory>();
this.Bind<IInstanceProvider>().To<StandardInstanceProvider>();

#if !SILVERLIGHT_20 && !WINDOWS_PHONE && !NETCF_35
this.Bind<IInterceptor>().To<FactoryInterceptor>()
    .When(request => typeof(IFactoryProxy).IsAssignableFrom(request.Target.Member.ReflectedType));
#endif

注意
如果实际使用FUNC-工厂,我会建议忽视的问题。现在你知道去哪里找,应该问题不断再出现。
。如果这是一个孤立的问题,你也可以取代你的 Func键<,> 通过一个接口,使事情更加明确(干净的代码)。
当然,这是不以任何方式一个完美的解决方案。这只是艰难的抉择,一个人做的一个; - )

Note: If you are actually using the Func-Factories, i would recommend "ignoring" the issue. You now know where to look, should the issue ever arise again. If this is an isolated problem, you could also replace your Func<,> by an interface, making things more explicit (clean code). Of course this is not a perfect solution by any means. It's just one of the tough choices one has to make ;-)

这篇关于从结合中停止Ninject Func <T,T,bool>自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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