singleton or value

0 views
Skip to first unread message

Richard

unread,
May 22, 2009, 6:48:40 AM5/22/09
to Smartypants IOC
Hi Josh

I'm loving Smartypants. Great work. One little question...

Is there any fundamental difference between

injector.newRule().whenAskedFor( MyInterface ).useSingletonOf
( MyClass );

and

injector.newRule().whenAskedFor( MyInterface ).useValue( new MyClass
() );

It seems to me there's no conceptual difference, but I just wanted to
check if the internal differences mean that one is preferable.

Thanks
Richard

Josh McDonald

unread,
May 22, 2009, 10:44:31 AM5/22/09
to smartyp...@googlegroups.com
There's a few differences:

1) When you use "useValue(new MyClass())" the class is created at the time you create your rules. When you use useSingletonOf(MyClass), the object is not created until it's requested. It's the same as lazy/eager singleton in Guice.

2) When you use usValue() and create the class yourself, your instance won't be injected into, as it will when you let SmartyPants take care of construction.

BTW, glad to hear you like it :)

-Josh

2009/5/22 Richard <ric...@bigroom.co.uk>



--
"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/

ric...@bigroom.co.uk

unread,
May 25, 2009, 6:11:46 AM5/25/09
to Smartypants IOC
Thank you.
Richard
Reply all
Reply to author
Forward
0 new messages