Need architecture guidance with Codecs and Codec Providers

We are considering migrating from ReactiveMongo to the official Mongo Scala Driver for one of our applications built upon Play Framework.

After reading the documentation thoroughly, we did not find any guidance regarding Codec / Codec Provider and Codec Registry definition.

The project is somehow complex, but we follow the MVC pattern. Each object which need to be stored in a ongo Database consists in:

  • A case class with a companion object;
  • A service class for taking care of the DAO.

In ReactiveMongo, best practices consist in implementing an implicit BSON Document Handler (equivalent to a Codec) in the companion object of each object to store in the MongoDB. The Service class automatically find the correct BSON Document Handler when serializing / deserializing object to / from BSON document.

What are the Mongo Scala Driver best practices regarding Codec and Codec Provider?

We found different solutions:

  • A global registry where all the codecs / codec providers are registered, which we find not ideal to maintain;
  • A per Service class codecs / codec providers registry definition, which we also find not ideal to maintain.

Is there a way to declare an implicit Codec or Codec Provider in each object companion class without having to explicitly declare the Code / Codec Provider in a local or global registry