Let there a few separate DAO classes OrderDAO
, ProductDAO
, and CustomerDAO
that store/retrieve data in the database and share a single instance DataSource
(the database connection factory).
In order to create a DataSource
instance and plug it in DAOs
we usually use Spring DI. Now I would like to do that in Scala without any DI framework.
I've read about the cake pattern, and it looks like I should do the following:
trait DatabaseContext { val dataSource:Datasource }
trait OrderDAO {this:DatabaseContext =>
... // use dataSource of DatabaseContext
}
trait ProductDAO {this:DatabaseContext =>
... // use dataSource of DatabaseContext
}
object DAOImpl extends OrderDAO with ProductDAO with DatabaseContext {
val dataSource = ... // init the data source
}
Do I understand the cake pattern correctly?
Can I implement these DAOs
differently using the cake pattern ?
What does it provide that DI frameworks like Spring do not ?
How can I create separate OrderDAOImpl
and ProductDAOImpl
objects sharing the same DataSource
instance instead of one big DAOImpl
?