InSingletonScope ... is this intended?

683 views
Skip to first unread message

Jörn von Holten

unread,
Aug 26, 2011, 5:45:42 AM8/26/11
to ninject
I am a newby with Ninject and are doing my first tries with you
central Shuriken/IWeapon/Samurai
example.

So I wanted to try that a Shuriken is definitly a singleton (for
conceptual understandings) so I tried

var samurai = kernel.Get<Samurai>();
var shuriken = kernel.Get<Shuriken>();

with the only bindings that seemed to me natural

1 Bind<Shuriken>().ToSelf().InSingletonScope();
2 Bind<IWeapon>().To<Shuriken>();

That creates two Shuriken instances. Hmmm. So I "refined" line 2 to
version 2a

1 Bind<Shuriken>().ToSelf().InSingletonScope();
2a Bind<IWeapon>().To<Shuriken>().InSingletonScope();

Again we get two Shuriken instances. My last try is version 2b

1 Bind<Shuriken>().ToSelf().InSingletonScope();
2b Bind<IWeapon>().ToMethod(ctx =>
ctx.Kernel.Get<Shuriken>());

and I got the desired behavour. I really had expected that 2 and 2b
are somehow equal.
But maybe this can be clarified for me.












Julian

unread,
Aug 26, 2011, 3:27:57 PM8/26/11
to nin...@googlegroups.com
Most of the time you want to bind an interface to an implementation.

Bind<IWeapon>().To<Shuriken>().InSingletonScope();

This will create a single Shuriken object which will be provided every time you request an injection of type IWeapon.

You me want to check out the following for more examples.

Cheers,
Julian

Jörn von Holten

unread,
Aug 28, 2011, 5:25:15 AM8/28/11
to ninject
That was not exactly my point. I was not talking about common
practice.
I thought about the difference of

Bind<IWeapon>().To<Shuriken>();

and

Bind<IWeapon>().ToMethod(ctx => ctx.Kernel.Get<Shuriken>());

which I expected to be equal but as described above they are not.
The paradigm behind ninject that is presented to be lightweight and
all that jazz seems
to have a reason for this I wanted to be clarified because I still
have no idea and so I
need that motivation.

Cheers
Jörn

Jörn von Holten

unread,
Aug 28, 2011, 5:28:57 AM8/28/11
to ninject
That was not exactly my point. I was not talking about common
practice.
I thought about the difference of

Bind<IWeapon>().To<Shuriken>();

and

Bind<IWeapon>().ToMethod(ctx => ctx.Kernel.Get<Shuriken>());

Julian

unread,
Aug 28, 2011, 4:08:09 PM8/28/11
to nin...@googlegroups.com
1           Bind<Shuriken>().ToSelf().InSingletonScope(); 
2a         Bind<IWeapon>().To<Shuriken>().InSingletonScope();
 

Two bindings are defined: 
Shuriken and IWeapon.
If you request the Shuriken type, you will always get the same object.
The same is true for the second binding IWeapon.
But those bindings do not share the objects they return.


Again, the common practice is to request an interface type.

1           Bind<Shuriken>().ToSelf().InSingletonScope(); 
2b         Bind<IWeapon>().ToMethod(ctx => ctx.Kernel.Get<Shuriken>()); 

The first line binds the Shuriken type like above.
The second line binds IWeapon to a method which requests exactly this type from the kernel.
So this time there will only be one 
Shuriken object.

Scopes are defined on a per-binding basis and bindings don't share their singleton objects.

I hope this makes sense.

Cheers,
Julian

Urs Enzler

unread,
Aug 29, 2011, 2:44:58 AM8/29/11
to ninject
Try this:

kernel.Bind<MyClass>().ToSelf().InSingletonScope();
kernel.BindInterfaceToBinding<IA, MyClass>();
kernel.BindInterfaceToBinding<IB, MyClass>();

This results in a "single" binding creating objects, thus only one
single instance of 'MyClass'.


The BindInterfaceToBinding extension method can be found in the
Ninject extension "ContextPreservation".

Cheers
Urs
Reply all
Reply to author
Forward
0 new messages