Generic ModelCompanion

21 views
Skip to first unread message

Pawan Kumar

unread,
Dec 10, 2013, 12:56:53 AM12/10/13
to scala...@googlegroups.com

Hi All,

Although the below code works after repeating it for various types of users such as APIUser, SystemUser etc.

object APIUser extends ModelCompanion[APIUser, ObjectId]{......}

object SystemUser extends ModelCompanion[SystemUser, ObjectId]{......}

Any thoughts on how to make it generic enough so that byId and other methods (common for all types of users) can be placed in one trait and used by all other user types?


Thanks

Pawan

------ 

@Salat

sealed trait User{

  def id: ObjectId

  def fullName: String 

  def email: String

}


case class APIUser(@Key("_id") id:ObjectId = new ObjectId, fullName:String, email:String,createdOn:DateTime = DateTime.now) extends User


object APIUser extends ModelCompanion[APIUser, ObjectId]{

  com.mongodb.casbah.commons.conversions.scala.RegisterConversionHelpers()

  com.mongodb.casbah.commons.conversions.scala.RegisterJodaTimeConversionHelpers()

  

  val mongocollection = MongoConnection()("db")("collection")

  val dao = new SalatDAO[APIUser, ObjectId](collection = mongocollection) {}

  def byId(id: String): Option[APIUser] = dao.findOne(MongoDBObject("_id" -> new ObjectId(id)))

}

Pawan Kumar

unread,
Dec 12, 2013, 6:21:42 AM12/12/13
to scala...@googlegroups.com
Found it..

Thanks
Pawan
Reply all
Reply to author
Forward
0 new messages