Let my application contain the business classes Order , Product , Customer , etc., and I would like to store / retrieve them from / from the database.
Usually for this purpose we use the DAO pattern. That is, we define the DAO interface and implementation for each business class: OrderDAO , ProductDAO , etc. Now I would like to use a template of type type:
trait DAO[T] { def create(t:T) ... // other CRUD operations } ... // DAO implementations for specific business objects implicit object OrderDAO extends DAO[Order] { def create(o:Order) {...} ... // other CRUD operations } ... // create a business object in the database def create[T](t:T)(implicit dao:DAO[T]) {dao.create(t)}
Now my problem is that all DAOs use an instance of DataSource (a factory of database connections), so I cannot define DAOs as objects . I have to create a single instance of the DataSource and pass it to all DAOs when they are initialized.
Let's say we have a function to create a DataSource :
def dataSource():DataSource = {...}
How do you implement
DAOs with class classes?
source share