问题标签 [ninject-2]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
asp.net-mvc-2 - Ninject 2.0:根据实现传递不同的参数
我刚刚开始使用带有 ASP.NET MVC 2 的 Ninject 2.0。所以,我有一个接口 IMongoRepository 和类 MongoRepository。
MongoRepository 接收参数字符串集合。
根据我要使用的集合,我在 MongoRepository 的参数中传入不同的值。我希望我的措辞正确,但是如何根据我使用的控制器映射不同的参数?
例如,在 Article 控制器中,我会调用:
在 PageController 我会调用:
我想做的只是进行构造函数注入并传入 IMongoRepository。有什么想法或建议吗?
顺便说一句,我只是在学习 IOC/DI;所以,我愿意接受国际奥委会忍者的任何提示!谢谢!
wcf - Ninject 2.0 的依赖注入
关于 Ninject 的一个小问题。
我使用 WCF“双工通道”与服务进行通信。通道被定义为一个接口,为了简单起见我们称之为它IMyChannel
。为了实例化一个通道,我们使用DuplexChannelFactory<IMyChannel>
对象的CreateChannel()
方法。到目前为止,我已经设法将工厂类与此绑定。
但是我有点不确定如何绑定IMyChannel
接口,因为我使用 Ninject 创建DuplexChannelFactory<IMyChannel>
所以绑定IMyChannel
我做Bind< IMyChannel>().
???
ioc-container - 在 Ninject 2.0 中,我如何同时拥有通用绑定和特定情况的绑定?
我有一种情况,我想依赖注入我的用户对象,但也将当前用户放在 IoC 容器中。我希望以下几行起作用:
有人可能会认为这样的绑定会起作用:
当然,这给出了:
我理解这个错误,因为命名绑定不限制该绑定的应用,所以两个绑定都适用。很明显,我需要将上下文绑定与 .When*() 方法一起使用,但我想不出任何方法来做到这一点。我觉得应该有检测是否应用了命名实例的方法。就像是:
我在界面上找不到任何地方,IRequest
或者它的属性告诉我请求的名称。我该怎么做呢?
dependency-injection - 如何在 Ninject 2 中使用公共服务定位器
Ninject 2 中的更改说Ninject支持Common Service Locator,但我该如何使用它呢?我没有找到任何手册或样本。
wcf - How to use Ninject2 with WCF?
In a short view of the IoC and DI .NET libraries I chose to use Ninject2. According to DDD style in:
- Infrastructure: I have Entity Framework 4.0 .edmx model and the Repository implementation
- Domain Layer: I have POCO objects and Repository Interfaces (implemented in Infrastructure)
- Application Services: I have WCF services that call methods defined in repository interfaces from Domain Layer.
My question is how to inject a Repository in WCF with Ninject2. I have read some articles with Ninject2 but are presented very simple example ( ex.: dependency injection in the same class library). Any indication would be useful:).
Many many thanks!
ninject - 引发与 OnePerRequestModule 相关的 Ninject 间歇性异常
我刚刚将 Ninject 的现有实现从 1.5 升级到了 2.0。我现在看到在短时间内发生许多请求时会引发间歇性异常。
这是抛出的异常。
类型: System.ArgumentException
消息:已添加具有相同键的项。
来源: Ninject
堆栈跟踪:
在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add 的 System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) (TKey key, TValue value)
at Ninject.Components.ComponentContainer.CreateNewInstance(Type component, Type implementation)
at Ninject.Components.ComponentContainer.ResolveInstance(Type component, Type implementation)
at Ninject.Components.ComponentContainer.Get(Type component)
在System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
在 System.Linq.Buffer`1..ctor(IEnumerable`1 源)
在 System.Linq 的 Ninject.Components.ComponentContainer.<CreateNewInstance>b__6(ParameterInfo 参数) .Enumerable.ToArray[TSource](IEnumerable`1 source)
在 Ninject.Components.ComponentContainer.CreateNewInstance(Type component, Type implementation)
在 Ninject.Components.ComponentContainer.ResolveInstance(Type component, Type implementation)
在 Ninject.Components.ComponentContainer。
在 Ninject.KernelBase.CreateContext的 Ninject.Components.ComponentContainer.GetT 处获取(类型组件)
(IRequest 请求,IBinding 绑定)
在 Ninject.KernelBase.<>c__DisplayClassa.<Resolve>b__6(IBinding binding)
at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext ()
在 System.Linq.Enumerable.<CastIterator>d__aa`1.MoveNext()
在 System.Linq.Enumerable.Single[TSource](IEnumerable`1 源)
在 Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter [] 参数)
在NameOfConsumingSite .Application_BeginRequest(Object sender, EventArgs e) ...
OnePerRequestModule
如果我从 web.config 中删除该项目,则不会引发异常httpModules
,但这会增加大量开销。
如果他们可以提供帮助,我提前感谢任何人!
ninject - 在 Ninject 2 中,我如何让两个具有不同设置的内核共享绑定?
我的应用程序有一个 Ninject 2 内核,其中包含所有绑定。应用程序的一部分需要在内核上具有与应用程序的其余部分不同的设置,但需要相同的绑定(该部分用于 NHibernate 和需要InjectNonPublic = true
和InjectAttribute
集合)。如何制作一个与当前内核共享绑定但具有不同设置的内核?
我相信在其他 IOC 容器中,这将通过“嵌套容器”来实现,但是我看不到 Ninject 中对嵌套容器的任何支持?
c# - Ninject V2 InRequestScope:在每个请求上触发构造函数
我很难使用 Ninject V2 的 InRequestScope() 来管理 UnitOfWork。
我希望每当我通过容器请求 UnitOfWork 时,始终会在整个请求中返回相同的对象。但是,在调试器中,我在对象的构造函数上有一个断点,每当对容器发出请求时,构造函数就会触发,就好像正在创建一个新对象一样。
有没有人有类似的经历,或者我错过了一些关键的东西?
dependency-injection - Ninject.Web.Mvc 附加组件不适用于 ASP.NET MVC 2
我正在使用Ninject.Web.Mvc
(MVC 2 版本)附加组件和 ASP.NET MVC 2。这是我的摘录Global.asax.cs
:
我也有一个基础RepositoryController
:
如您所见,这是一个非常简单的设置,RepositoryController
需要注入 的实例IRepository
,而 Ninject 被配置为使用NHibernateRepository
. 但是,这不起作用,并且Repository
每当我尝试在控制器中访问它时该属性为空。但是,如果我将代码更改为此:
然后它工作正常。有谁知道为什么构造函数注入不起作用,但属性注入是?
ninject - Ninject 2 中的上下文变量
我在早期版本的 Ninject 中找到了这篇关于上下文变量的文章。我的问题有两个。首先,如何使用 Ninject 2 获得这种行为?其次,上下文变量是否沿着请求链进行?例如,假设我想替换这些调用:
... 有了这个:
是否可以设置这样的绑定,当需要构造 a 时,上下文会记住它处于“特殊”上下文中C
?