Ninject 3个多重绑定 [英] Ninject 3 multiple bindings

查看:89
本文介绍了Ninject 3个多重绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题实际上是这里发布的一个旧问题的重复: Ninject 2.2多个绑定

My question is really a repeat of an old question posted here: Ninject 2.2 multiple bindings

似乎有人打算在2011年解决这个问题.有人知道Ninject是否有某种方法可以关闭此类警告?还是其他解决方法?

It seems someone was going to deal with this back in 2011. Does anyone know if there is some way to turn off such warnings in Ninject? Or some other workaround?

编辑

响应@BatteryBackupUnit,这是我的确切问题:

In response to @BatteryBackupUnit, here is my exact problem:

我有多个库...并且在我的核心库中,我做这样的事情:

I have multiple libraries... and in my core library, I do something like this:

  1. 查找主机应用程序(包括主机)引用的所有程序集
  2. 从所有这些程序集中找到从IDependency继承的所有类型.
  3. 自动将所有这些都注册为瞬态
  1. Find all assemblies referenced by the host application (including the host)
  2. Find all types inheriting from IDependency from all those assemblies.
  3. Automatically register all of those as transient

然后从另一个库(主机应用程序可能引用也可能未引用)中,我有了这个:

Then from another library (which may or may not be referenced by the host app), I have this:

Kernel.Bind<IDbContextFactory>().To<DbContextFactory>().InSingletonScope();

这里IDbContextFactory也是IDependency,所以它已由核心库加载,现在我在这里注册了它,但是具有不同的作用域(单例).

Here IDbContextFactory is also an IDependency, so it got loaded already by the core library and now I register it here but with a different scope (singleton).

根据经验(并且已经进行了较早的测试),我知道这在Autofac中是没有问题的,但是Ninject给我有关已注册它的错误消息.

From experience (and having tested it earlier) I know this is no problem in Autofac, but Ninject gives me that error message about having already registered it.

理想情况下,最好改写以前的所有注册信息…层叠样式"(由于缺少更好的用语).

Ideally it would be better to just override any previous registrations... "cascade style" (for lack of a better phrase)..

推荐答案

Ninject现在支持使用更具体的绑定覆盖开放的通用绑定. 例如:

Ninject does now support overriding open generic bindings with more specific ones. For Example:

public interface IFoo<T> { }
public class Foo<T> : IFoo<T> { }
public class StringFoo : IFoo<string> {}

使用方式:

var kernel = new StandardKernel();
kernel.Bind(typeof(IFoo<>)).To(typeof(Foo<>));
kernel.Bind<IFoo<string>>().To<StringFoo>();

var intFooInstance = kernel.Get<IFoo<int>>();
var stringFooinstance = kernel.Get<IFoo<string>>();

工作.

但是,如果您不是在谈论开放的通用绑定,ninject 3仍然会像ninject 2.2一样处理多重绑定.

However, if you're not talking about open generic bindings, ninject 3 still handles multi bindings the same as ninject 2.2.

在大多数情况下,您可以通过使用上下文绑定来解决此问题.好吧,我不会确切地称它为解决方法,我会称其为良好的设计. 通常在这里对此进行描述: https://github.com/ninject/ninject/wiki/上下文绑定

In most scenarios you can work around this by using contextual bindings. Okay i would not exactly call it a workaround, i would call it good design. In general this is described here: https://github.com/ninject/ninject/wiki/Contextual-Binding

一种简单的方法是使用名称指定绑定.这需要为指定的绑定一个绑定,并且也仅允许一个绑定. 请参阅: https://github.com/ninject /ninject/wiki/Contextual-Binding#simple-constrained-resolution-named-bindings

A simple way would be to specify the binding using a name. This requires one binding for the specified one and allows only one, too. See: https://github.com/ninject/ninject/wiki/Contextual-Binding#simple-constrained-resolution-named-bindings

还可以定义默认"绑定(如.Bind<IFoo>().To<Foo>();)和特殊情况下的绑定,如.When(...)语法,如:

It is also possible to define a "default" binding like .Bind<IFoo>().To<Foo>(); and special case bindings with the .When(...) syntax, like:

.Bind<IFoo>().To<SpecialFoo>().When(ctx => ...)

请参见如果您向我们展示您的具体问题,我们也许可以提供更具体的解决方案.

If you show us your concrete problem we might be able to provide a more concrete solution.

这篇关于Ninject 3个多重绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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