如何使用Ninject的IsSingletonScope方法?
我的项目中有以下 Ninject
绑定。
Bind<IThingsDataContext>().To<ThingsDataContext>().InSingletonScope();
Bind<IThingViewModel>().To<ThingViewModel>();
Bind<IThingsListViewModel>().To<ThingsListViewModel>();
我需要通过构造函数将 ITingsDataContext
注入到 ThingViewModel
和 ThingsListViewModel
,并且它与 ITingsDataContext
的实例相同。
但是当我这样做时,
_kernal.Get<IThingViewModel>();
_kernal.Get<IThingsListViewModel>();
我看到两个不同的 ITingsDataContext
实例注入到视图模型中! 我是否对绑定做了错误的操作或者错误地使用了 IsSingletonScope?
I have a follwing Ninject
bindings in my project.
Bind<IThingsDataContext>().To<ThingsDataContext>().InSingletonScope();
Bind<IThingViewModel>().To<ThingViewModel>();
Bind<IThingsListViewModel>().To<ThingsListViewModel>();
I need to inject IThingsDataContext
to ThingViewModel
and ThingsListViewModel
through constructor, and it has be the same instace of IThingsDataContext
.
But when I do
_kernal.Get<IThingViewModel>();
_kernal.Get<IThingsListViewModel>();
I see two different instance of IThingsDataContext
injected to the viewmodels!
Am I doing something wrong with the bindings or using IsSingletonScope
incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚重新开始使用 Ninject,所以我有点生疏,但这听起来像是我从 Ninject 1.0 迁移到 2.0 时遇到的单例问题。您可以在我的博客上阅读更多详细信息,但我认为你想首先在单例上下文中将 ThingsDataContext 绑定到自身。然后,您可以将 IThingsDataContext 绑定到内核中的 ThingsDataContext 副本。
I'm just getting back into using Ninject so I'm a little rusty, but this but this sounds like the singleton problem I encountered when moving from Ninject 1.0 to 2.0. You can read more details on my blog, but I think you want to bind ThingsDataContext to itself in singleton context first. Then you can bind IThingsDataContext to the copy of ThingsDataContext that is in the kernel.