Let's consider a simple example of DAO pattern. Let Person is a value object and PersonDAO is the correspondent trait, which provides methods to store/retrieve Person to/from the database.
trait PersonDAO {
def create(p:Person)
def find(id:Int)
def update(p:Person)
def delete(id:Int)
}
We use this pattern (as opposed to Active Record, for example), if we want to separate the business domain and persistence logic.
What if we use another approach instead ?
We will create PersonDatabaseAdapter
trait PersonDatabaseAdapter{
def create
def retrieve(id:Int)
def update
def delete
}
and implicit conversion from Person to it.
implicit def toDatabaseAdapter(person:Person) = new PersonDatabaseAdapter {
def create = ...
def retrieve(id:Int) = ...
def update = ...
def delete = ...
}
Now if we import these conversions, we can write client code to manipulate Persons and store/retrieve them to/from the database in the following manner:
val person1 = new Person ... person1.create ... val person2 = new Person ... person2.retrieve(id) ...
This code looks like Active Record but the business domain and persistence are still separated.
Does it make sense ?