【问题标题】:Question about Cake Pattern关于蛋糕图案的问题
【发布时间】:2011-08-06 12:09:46
【问题描述】:

让几个单独的 DAO 类 OrderDAOProductDAOCustomerDAO 在数据库中存储/检索数据并共享单个实例 DataSource(数据库连接工厂)。

为了创建DataSource 实例并将其插入DAOs,我们通常使用Spring DI。现在我想在没有任何 DI 框架的情况下在 Scala 中做到这一点。

我已阅读有关 cake pattern 的信息,看来我应该执行以下操作:

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
}

我是否正确理解蛋糕图案?

我可以使用蛋糕模式以不同的方式实现这些DAOs 吗?

它提供了什么是像 Spring 这样的 DI 框架没有的?

如何创建单独的 OrderDAOImplProductDAOImpl 对象共享同一个 DataSource 实例而不是一个大的 DAOImpl

【问题讨论】:

  • 我简要地阅读了有关蛋糕图案的信息,但也没有看到令人兴奋的地方。它似乎比现有的 DI 容器复杂得多。
  • 您是否喜欢它与问题并不真正相关。 “我错过了什么”是一个非常弱的问题。也许最好问“这提供了什么而 X 没有?”,这是一个明确的问题。
  • @丹尼尔。谢谢。我已经改写了这个问题。

标签: scala dependency-injection dao


【解决方案1】:

蛋糕图案的优点是:

  • 与基于配置文件的 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.//

【讨论】:

    【解决方案2】:

    也许:

    • 在编译时静态检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      相关资源
      最近更新 更多