Getting current Play Application's injector

257 views
Skip to first unread message

Chung Onn Cheong

unread,
Jul 5, 2015, 2:23:57 AM7/5/15
to sca...@googlegroups.com
Hi,

I am moving from Play's DI to Scaldi, for most part of the conversion to Scaldi it is quick and easy. However, I am not able to get a Scaldi replacement for Play's  play.api.Play.current.injector.instanceOf[T] from the scaladoc. I don't want to use the trait scaldi.Injectable as I need to add an implicit arg list as the class that needs the injector is deeply nested.

Any help is much appreciated

Regards
chungonn

Oleg Ilyenko

unread,
Jul 5, 2015, 3:45:32 AM7/5/15
to Chung Onn Cheong, sca...@googlegroups.com
Hi,

The current equivalent would be `ScaldiSupport.currentInjector`. `play.api.Play.current.injector.instanceOf[T]` should also work, but of course you will not be able to use it with `Injectable`, since it's part of the play API. 

One warning though, `ScaldiSupport.currentInjector` is deprecated and probably will be removed in the future versions since it ment to be used by old api which are not aware of the new DI mechanism of play. 

So I would recommend you to avoid using both: ` Play.current` and `ScaldiSupport.currentInjector` in favor of using DI capabilities of Scaldi and possibly adding bindings for all of the nested classes you mentioned or passing down the implicit injector to them. 

That said, if you feel, that these approaches do not suit you application, I would be interested to hear more about it in order to better understand the use-case and possibly find/implement a better solution for it. 

Cheers,
Oleg
--
You received this message because you are subscribed to the Google Groups "scaldi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scaldi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chung Onn Cheong

unread,
Jul 5, 2015, 5:07:46 AM7/5/15
to Oleg Ilyenko, sca...@googlegroups.com
Hi Oleg,

Thanks for the quick response. I needed Scaldi injector instead of Play's is because I would like to use the cool stuffs that your library provides especially the  ability to bind to a function. Yes, I read  the warning about ScaldiSupport being deprecated and it may be removed in future. 

In my situation, I have a multi-project scenario, this base project defines a Page which has a configurable layout, it also provides a default implementation and the depending or top project may chose to override it. I have been avoiding using DI in favor of Scala self-type and plain old factory pattern as I previously had a "wiring-hell"  using Guice in my past java projects. 

I find the need "retrofit" all the calling functions with an implicit injector when I need to inject[T] is restrictive or even not feasible in situation where I wanted to subclass a 3rd party library and I needed the DI magic. However, I do find having either` Play.current` and `ScaldiSupport.currentInjector`  appears to be a bit hacky especially it is sprinkled all over the code. 

Finally, I find Scaldi very clean and easy to use and getting it to work with Play is plain simple. Thanks for giving us this nice library.

Regards,
chungonn

Alexandre Bedrytski

unread,
Jul 6, 2015, 1:58:19 AM7/6/15
to sca...@googlegroups.com, chun...@gmail.com
Hi,

I wonder if it is possible to inject things (like config values) from within a trait. Without an explicit parameter, how can one have access to the injector?

Alex.
To unsubscribe from this group and stop receiving emails from it, send an email to scaldi+unsubscribe@googlegroups.com.

Chung Onn Cheong

unread,
Jul 6, 2015, 2:05:21 AM7/6/15
to Alexandre Bedrytski, sca...@googlegroups.com
Hi Alex,

I assume you are referring to reading  Typesafe config files. With my 2 days old scaldi knowledge, I think you can use  TypesafeConfigInjector to create an injector, checkout link here -  http://scaldi.org/learn/#typesafe-config-injector

HTH

chungonn

To unsubscribe from this group and stop receiving emails from it, send an email to scaldi+un...@googlegroups.com.

Aliaksandr Bedrytski

unread,
Jul 6, 2015, 2:11:41 AM7/6/15
to Chung Onn Cheong, sca...@googlegroups.com
Hi Chung,

you still need to get an implicit injector from somewhere to get config values with TypesafeConfigInjector. Traits cannot have constructor parameters so extending Injectable is useless.
And without implicit injector, no access to injectable config parameters.

Alex

Chung Onn Cheong

unread,
Jul 6, 2015, 8:11:24 AM7/6/15
to Aliaksandr Bedrytski, sca...@googlegroups.com
Hi Alex,

Sorry, I just tested it, I was not able to get it working too. When I have more time, I will try it again.

chungonn

Oleg Ilyenko

unread,
Jul 9, 2015, 2:27:37 PM7/9/15
to Chung Onn Cheong, sca...@googlegroups.com
Hi,

Sorry for the late reply and thanks a lot for the kind words!

Even though `ScaldiSupport` trait is deprecated now and probably would be removed in future,  the `currentInjector` can still live in some other place. I haven't decided yet. 

On one hand, I also dislike the concept of `currentInjector`. But on the other hand there are use-cases like this. And I also don't want to make scaldi too prescriptive, so it's a tough choice :)
Reply all
Reply to author
Forward
0 new messages