Skip to content

Instantly share code, notes, and snippets.

@remeniuk
Created April 21, 2011 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remeniuk/933872 to your computer and use it in GitHub Desktop.
Save remeniuk/933872 to your computer and use it in GitHub Desktop.
Cake pattern for Spring-flavored DAOs
import java.net.URI
trait Order
trait Product
trait Datasource
trait DAO[T] {
def create(t:T)
}
trait JdbcSupport {
val dataSource:Datasource
def init(uri:URI, user:String, password:String) = Unit
}
trait OrderDAO {self: JdbcSupport =>
val orderDao: DAO[Order]
class OrderDAOImpl extends DAO[Order]{
//...
def create(t:Order) = {
dataSource//...
}
}
}
trait ProductDAO {self: JdbcSupport =>
val productDao: DAO[Product]
class ProductDAOImpl extends DAO[Product]{
//...
def create(t:Product) = {
dataSource//...
}
}
}
object DAOLayer extends OrderDAO with ProductDAO with JdbcSupport {
val dataSource = new Datasource{}
//init(uri, user, password)
val productDao = new ProductDAOImpl
val orderDao = new OrderDAOImpl
}
class ProductService {
import DAOLayer._
def createProduct(product: Product) = productDao.create(product)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment