mercredi 7 décembre 2016

scala - How to separate public API from implementation while providing a default one

I would like to develop a framework in which users may inject their own code inside our platform, without knowing some of our implementation details that may change in near future (e.g Database type).

I have splitted the code into two distinct projects:

  • One containing public APIs
  • One containing the server-side implementation

However, I would like they can test their code, with a kind of default implementation of some of this API.

For instance, in the public API:

trait UserDatabase {
  def getAllUserInfo : List[UserInfo]
  ...
}

trait ExecutesOnServer{
  def doWhaterverYouWant(db <: UserDatabase ) : Unit
}

In the user code:

class ExecutesOnServerImpl{
  @override def doWhaterverYouWant(db <: UserDatabase) : Unit {
    db.getAllUserInfo.foreach(println)
  }
}

Now in the private implementation:

class MySQLUserDatabase extends UserDatabase {
  @override def getAllUserInfo : List[UserInfo] = mysql.query(...)
  ...
}

I would like the user to have access a kind of default impl like LocalTextFileUserDatabase to test their code, and this is replaced my the server side one on the server.

What is the best way to do this ?

Thanks for your help

Aucun commentaire:

Enregistrer un commentaire