problemi elementari - connessioni ai db nei funzionali

51 views
Skip to first unread message

ildella

unread,
May 14, 2015, 8:17:17 AM5/14/15
to sug...@googlegroups.com
Ciao a tutti.

Il problema e' banale ma evidentemente mi manca un sacco di contesto lavorando con i funzionali.

Seguendo la doc di ReactiveMongo, ma credo il problema sia simile per qualsiasi db:
https://github.com/ReactiveMongo/ReactiveMongo
loro fanno un bravo metodo connect() che alla fine mi porta ad avere la collection.
poi la collection la uso in un altro metodo, per leggere o scrivere.

ora in questo secondo metodo, la collection dovrebbe essere una, orrore, variabile di classe? In Java o simili sarebbe stato cosi'.
Qui invece come dovrei fare?

Shirish Padalkar

unread,
May 14, 2015, 8:29:06 AM5/14/15
to sug...@googlegroups.com
Hi,

I have used Google Translator for translation, so please pardon me if I misunderstood your question.

If I understood you correctly, you are asking if collections should be class variables.

Play-reactivemongo actually recommends it to be a def instead of val. You can refer the documentation here - https://github.com/ReactiveMongo/Play-ReactiveMongo


Otherwise, if you are not using it inside Play application, I don't see any problems having it as a private val inside a class.


With regards,
Shirish (http://twitter.com/_Garbage_)


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

ildella

unread,
May 14, 2015, 9:42:24 AM5/14/15
to sug...@googlegroups.com
Hi,

that was my point, I do not want to make it a class variable but I do not know how to write it properly using a val.
Now if I can read properly, the line:

  def collection: JSONCollection = db.collection[JSONCollection]("persons")

db is injected somehow from the application.conf
declaring "def collection" is what makes the trick to have the "collection" variable always available in the code.

my problem was very level, like how can I access a val in different method without having to call all the connection.db and db.collection all the time...
Probably the driver is smart enough... Anyway I'll follow that thanks.


Marco F.

unread,
May 14, 2015, 5:57:56 PM5/14/15
to sug...@googlegroups.com
What I'd do is have connection/collection being handled by an Object.

Something along these lines:

object MongoHelper {
  import reactivemongo.api._
  import scala.concurrent.ExecutionContext.Implicits.global

  val driver = new MongoDriver
  val connection = driver.connection(List("localhost"))

  val db = connection("my-db") 
  val collectionA = db("acoll")
}


So then I can just import the Object from somewhere else and access collectionA from it.

How does that look to you guys?

-marco

Shirish Padalkar

unread,
May 15, 2015, 2:38:52 AM5/15/15
to sug...@googlegroups.com
Well, that sounds okay.

My worry is, it unnecessarily presents db, driver and connection to the client. While I understand there might be ways to get to the driver from collectionA, I think these are more details than required for the client which just want to have access to collection. Of course, it can be solved by making them private. :)

When I am using dependencies injection, my approach is almost same. I declare it a singleton class with all internal variables private and only expose defs for collections. This object can then be injected to any controller / service easily.
Another approach is using cake-pattern, but I think it's too much hassle. ;)


With regards,
Shirish (http://twitter.com/_Garbage_)


Diego

unread,
May 15, 2015, 1:06:50 PM5/15/15
to sug...@googlegroups.com
using cake pattern something like this I think:

trait ConnectionComponent {
   def connection: Connection
}

trait ConnectionComponentImpl extends ConnectionComponent {
   lazy val connection: Connection = { ..IMPL } //you can override def with val or keep def
}

trait UserServiceImpl extends UserService { this : ConnectionComponent =>
    def findUser(id:Long) = //IMPL and access to connection
}

object Application {
     val app = new UserServiceImpl with ConnectionComponentImpl
     
     app.findUser(1l) //OK
Reply all
Reply to author
Forward
0 new messages