useSingletonOf

3 views
Skip to first unread message

Nek

unread,
Sep 30, 2009, 2:42:48 PM9/30/09
to Smartypants IOC
Hi!
I've got a little question on injector behaviour.

When I use:
SmartyPants.whenAskedFor(INetStreamProvider).useSingletonOf
(VideoStreamPlayer);
SmartyPants.whenAskedFor(IVideoStreamPlayer).useSingletonOf
(VideoStreamPlayer);

There are two different instances of VideoStreamPlayer injected.

However. When I use:
SmartyPants.whenAskedFor(INetStreamProvider).useSingletonOf
(VideoStreamPlayer);
SmartyPants.whenAskedFor(IVideoStreamPlayer).useRuleFor
(INetStreamProvider);

There is one instance of VideoStreamPlayer.

Is the first one expected behaviour?

Josh McDonald

unread,
Sep 30, 2009, 6:04:39 PM9/30/09
to smartyp...@googlegroups.com
Yeah, that's the intended behaviour the reason is that the rule is assosciated to the key class, not the actual impl class. However, if you do:

SmartyPants.whenAskedFor(VideoStreamPlayer).useSingleton();

(or add [Singleton] to VideoStreamPlayer)

And then create your other two rules like this, it should only create one instance. If not, it's a bug :)

SmartyPants.whenAskedFor(INetStreamProvider).useClass(VideoStreamPlayer);
SmartyPants.whenAskedFor(VideoStreamPlayer).useClass(VideoStreamPlayer);

The rules:
.useSingleton() creates an instance per key
.useClass() looks up the impl class and attempts whatever is specified in the rules (including the default rule)
.createInstanceOf(Foo) doesn't process any more rules about Foo, and always creates a new instance of the specified impl class.

-Josh

2009/10/1 Nek <nikd...@gmail.com>



--
"Therefore, send not to know For whom the bell tolls. It tolls for thee."

Josh 'G-Funk' McDonald
  -  jo...@joshmcdonald.info
  -  http://twitter.com/sophistifunk
  -  http://flex.joshmcdonald.info/

Nikita Dudnik

unread,
Sep 30, 2009, 8:27:43 PM9/30/09
to smartyp...@googlegroups.com
Thanks!
Really useful.

Josh McDonald

unread,
Sep 30, 2009, 8:46:19 PM9/30/09
to smartyp...@googlegroups.com
No problems, I've made a note to better document that in the Wiki / site.

2009/10/1 Nikita Dudnik <nikd...@gmail.com>
Reply all
Reply to author
Forward
0 new messages