Play 2.4 Dependency Injection in Non-Controller Modules

186 views
Skip to first unread message

kraythe

unread,
Apr 17, 2015, 5:18:22 PM4/17/15
to play-fr...@googlegroups.com
Greetings, 

I am working on getting a Play 2.3.8 App working in 2.4 and I am having a problem I couldnt find documentaiton for. 

I have a class that records modifications to a user wallet as transactions in the system. Right now I look up those transaction descriptions in the messages API like so: 

class Wallet {
 
def depositFundsFromCAMS(usdCents: Long)
    val description
= MessagesApi("txn.cams.deposit", ccLastFour)
    val txn
= UserWalletTxn.createCamsTxn(this, user, description, usdCents)
     
// and so on...
 
}
}



Now in the new Play 2.4 the messages is an Injected API but this code is not injected, created by play or otherwise. It is an entity object stored in persistence. Are the APIs that were injected now not available ot anything not created by the injector? That would be a serious problem because we would have to re-implement the architecture. I am not a big fan of Anemic domain models (Object Oriented means data + behavior, C structs are a bummer). 

How would I implement this in the new Play? What is the proscribed recomended process? 

Thanks 

Julien Richard-Foy

unread,
Apr 20, 2015, 3:42:16 AM4/20/15
to play-fr...@googlegroups.com
Hi,

I would say that this is a limitation of your persistence library. See in its documentation if it is able to inject dependencies of entities.

Julien Richard-Foy

unread,
Apr 20, 2015, 3:43:47 AM4/20/15
to play-fr...@googlegroups.com
Well, it seems that it is not recommended to use dependency injection with your persistent entities: http://stackoverflow.com/questions/11638311/can-you-use-dependency-injection-in-persistent-entities

kraythe

unread,
Apr 21, 2015, 1:10:40 AM4/21/15
to play-fr...@googlegroups.com
I guess the real question is whether the dependencies can be looked up rather than injected. If so, how is that done? 

James Roper

unread,
Apr 21, 2015, 7:31:25 AM4/21/15
to play-framework
Most APIs have been made as backwards compatible as possible.  Where they aren't, you can use:

play.api.Play.current.injector.instanceOf[SomeComponent]

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



--
James Roper
Software Engineer

Typesafe – Build reactive apps!
Twitter: @jroper
Reply all
Reply to author
Forward
0 new messages