简体   繁体   中英

implicit declaration for database session playframework

I'm working on a project with scala playframework and I implicitly declared the database connection session so that I can read/write to mysql database without having to call the database session every time. Is this a good practice?

Is there any trade off?

Thanks

Working with implicit sessions is certainly preferable so the framework can manage those details for you. The only real downside is needing (implicit s: Session) on all your methods, but this can be worked around .

There is also the added benefit of making things more "pluggable" during testing.

Passing "contextual" information around via implicits is a reasonable practice. You can even see this with the ExecutionContext passed to futures in the Scala standard library.

It's definitely better than global values or pseudo-global values in a singleton object, and it makes things easier to mock when testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM