An implicit connection for the whole system/project & FutureConverter

34 views
Skip to first unread message

Vladimir Kostyukov

unread,
Apr 9, 2014, 12:07:39 PM4/9/14
to reacti...@googlegroups.com
Hi Stephane and ReactiveMongo users,

I've been working on a project that combines Twitter's Finagle and ReactiveMongo Driver. I managed to solve two interesting issues: (a) converting Scala Future to Twitter's Future and (b) an implicit mongo connection for a whole project. I decided to put my findings here. Hope it will help someone.


The 'FutureConverter' class might be used as follows:

val s = new Service[Req, Rep] with FutureConverter {
  def apply(req: Req): Future[Rep] = {
    val f = ... // a Scala Future returned by ReactiveMongo
    f.toTwitterFuture
  }
}


The documentation says there should a singleton 'MongoConnection' in the system. I found a truly Scala way of handling that. This is especially useful when the functionality is composed by services/modules which are Scala's objects. The usage example looks like this (example of usage with Finagle's Service):

object FooService extends Service[Request, Response] with ImplicitMongoConnection[Future[Response]] { // we have to explicitly import implicit value into the scope // we can do it at 'file' scope level import Main._ def apply(req: Request): Future[Response] = withConnection { implicit connection => // do all the magic here with 'connection' // and pack the result into a future Future.never } }

The question to Stephane. Is it possible to have an ImplicitMongoConnection trait out-of-the-box in further releases. I found this tiny class really useful.

Stephane Godbillon

unread,
Apr 16, 2014, 2:32:52 AM4/16/14
to reacti...@googlegroups.com
Hi Vladimir,

Thanks for sharing.  I'm not sure if ImplicitMongoConnection should be included directly in the library though; this use case seems a pretty specific and this trait is easy to write :)

Cheers,

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

Reply all
Reply to author
Forward
0 new messages