开发者

How to use Ninject's IsSingletonScope method?

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?


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.

Bind<ThingsDataContext>().ToSelf().InSingletonScope();
kernel.Bind<IThingsDataContext>.ToMethod( c => c.Kernel.Get<ThingsDataContext>());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜