如何解决有关蛋糕图案的问题
| 让几个单独的DAO类OrderDAO
,ProductDAO
和CustomerDAO
存储/检索数据库中的数据并共享一个实例DataSource
(数据库连接工厂)。
为了创建DataSource
实例并将其插入DAOs
中,我们通常使用Spring DI。现在,我想在没有任何DI框架的Scala中做到这一点。
我已经读过蛋糕模式,看来我应该执行以下操作:
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
}
我是否正确理解蛋糕图案?
我可以使用蛋糕图案以不同的方式实现这些“ 5”吗?
它提供了像Spring这样的DI框架不提供什么?
如何创建单独的OrderDAOImpl
和ProductDAOImpl
对象,它们共享相同的DataSource
实例,而不是一个大的DAOImpl
?
解决方法
蛋糕模式的优点是:
与基于配置文件的DI解决方案不同,将合同匹配到
实现是在编译时完成的,这减少了类查找
和兼容性问题。但是,许多DI引擎都有
替代的代码内配置功能
没有第三方库
被使用。让您使用模式的自类型注释是
母语功能。无需特殊语法即可检索
合同执行
忘记指定
实现另一个组件所需的组件会导致
运行时错误-只需查看本文
http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di.html
并尝试不指定组件之一或指定
特质,而不是任何蛋糕图案中的具体类别
示例,甚至忘记初始化与所需组件相对应的val
但是,要体验这些优点,您需要更严格地遵循模式的体系结构-检查同一篇文章,并注意包含实际合同和实现的包装特征。
您的示例似乎并不是严格意义上的蛋糕模式。在您的情况下,您可以仅使用继承为特征创建实现,并为每个DAO组件使用单独的类。在蛋糕模式中,消费代码将像DAO代码一样是一个组件,并且将依赖项组合在一起的代码将独立于此。
为了说明蛋糕模式,您必须在示例中添加使用类(域层或UI层)。或者,如果您的DAO组件访问彼此的功能,则可以单独说明DAO上的蛋糕模式。
简而言之,
trait OrderDAOComponent {
val dao: OrderDAO
trait OrderDAO {
def create: Order
def delete(id: Int): Unit
//etc
}
}
trait OrderDAOComponentImpl extends OrderDAOComponent {
class OrderDAOJDBCImpl extends OrderDAO {
def create: Order = {/*JDBC-related code here*/}
def delete(id: Int) {/*JDBC-related code here*/}
//etc
}
}
//This one has a dependency
trait OrderWebUIComponentImpl {
this: OrderDAOComponent =>
class OrderWebUI {
def ajaxDelete(request:HttpRequest) = {
val id = request.params(\"id\").toInt
try {
dao.delete(id)
200
}
catch {
case _ => 500
}
}
}
}
//This matches contracts to implementations
object ComponentRegistry extends
OrderDAOComponentImpl with
OrderWebUIComponentImpl
{
val dao = new OrderDAOJDBCImpl
val ui = new OrderWebUI
}
//from some front-end code
val status = ComponentRegistry.ui.ajaxDelete(request)
有关您的示例的更多信息。我认为,在以下情况下,它可能更像蛋糕:
trait DatabaseContext { val dataSource:Datasource }
trait OrderDAOComponent {this:DatabaseContext =>
trait OrderDAOImpl {
... // use dataSource of DatabaseContext
}
}
trait ProductDAOComponent {this:DatabaseContext =>
trait ProductDAOImpl {
... // use dataSource of DatabaseContext
}
}
object Registry extends OrderDAOComponent with ProductDAOComponent with DatabaseContextImpl {
val dataSource = new DatasourceImpl //if Datasource is a custom trait,otherwise wrapping needed
val orderDAO = new OrderDAOImpl
val productDAO = new ProductDAOImpl
}
//now you may use them separately
Registry.orderDAO.//
,也许:
在编译时进行静态检查。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。