0

我正在寻找一些迷你模式:该程序应该能够支持各种格式作为输入,然后应用转换并在最后一步将它们加载到数据库中。

其主要目的是提供测试数据。

我最初的想法是像这样将不同的组件“粘合”在一起:

我们有一个提取器,它从通用数据源 [A] 提取到 [B] 的迭代器,然后是一个将 [B] 映射到 [C] 的转换器,最后是一个将 [C] 加载到数据库中的步骤。我确信必须有更好的方法来解决这个问题。有没有更好的,可能更通用的方法来实现这一目标?

trait Importer[A, B, C] {

  val extractor: Extractor[A, B]
  val transformer: Transformator[B, C]
  val loader: Loader[C]

  /**
   * this is the method call for chaining all events together
   */
  def importAndTransformData(dataSource: A): Unit =
    {
     /**
      * extraction step
      */
      val output = extractor.extract(dataSource: A)

      /**
       * conversion method
       */
      val transformed = output map (transformer.transform(_))

      /**
       * loading step
       */
      transformed.foreach(loader.load(_))
    }

}

最诚挚的问候,

斯特凡

4

1 回答 1

0

Scala 中使用的一种常用方法是自键入(尤其是在Cake Pattern中使用的)。在你的情况下,这看起来像:

trait Importer[A, B, C] {
  self: Extractor[A, B] with Transformator[B, C] with Loader[C] =>

  /**
   * this is the method call for chaining all events together
   */
  def importAndTransformData(dataSource: A): Unit =
    {
     /**
      * extraction step
      */
      val output = extract(dataSource: A)

      /**
       * conversion method
       */
      val transformed = output map (transform(_))

      /**
       * loading step
       */
      transformed.foreach(load(_))
    }

}

然后,您可以使用以下代码构建您的 Importer:

val importer = new Importer with FooExtractor with FooBarTransformer with BarLoader {}

或者

val testImporter = Importer with MockExtractor with TestTransformer with MockLoader {}

或类似的测试用例。

于 2013-10-14T22:48:34.403 回答