Hi,
I'm looking into making our Play 2 application multi-tenant, in such a
way that it would use different connections (to different DBs)
depending on which subdomain / URL is accessed (customer1.foo.org,
customer2.foo.org, ...).
We've been using SalatDAO so far, and I see there's now also a
ModelCompanion trait that adds additional functionality.
I've been trying to think of an easy way to keep the straight-forward
way of accessing the DAO companion object directly ( e.g.
User.find(...) ) and yet switch to the correct connection behind the
scenes.
What I've been thinking about is to make use of implicit parameters, like e.g.
case class User(...)
object User extends ModelCompanion[User, ObjectId] {
def dao(implicit subdomain: String) = { ... }
def findAll()(implicit subdomain: String) = dao.find(MongoDBObject())
}
subdomain could be anything really, the crux is that if it is in
scope, when a method of the User DAO is called, it'd be able to pass
that key along and dao could resolve dynamically to whatever is
suited.
Does anyone think this kind of design is a good idea, and is there
perhaps something more clever (right now you've got to include that
implicit parameter all over, but I can't think of a better way atm)?
I've been thinking of giving this a go by implementing a delegating
DAO trait that'd be used in all cases. Note that I'd probably use that
directly instead of using the ModelCompanion (we're not using it,
since we don't need the JSON capabilities).
Thanks,
Manuel