2

我想知道是否有人可以分享一些使用 SORM 的最佳实践,参考在参与者系统中使用和自动化测试(单元/功能)。

你怎么:

  • 从运行时构建的外部化配置(例如 typesafe 的 application.conf)驱动您的 DB/Instance 单例
  • 做依赖注入(构造函数或蛋糕模式或?)以使用与生产/测试数据库相同的代码..还是完全使用另一种策略?

似乎当我没有将我的“实例”声明为单例(对象扩展实例)而是创建一个新实例以注入我的演员进行测试时,我遇到了使用 SORM 的奇怪堆栈跟踪。我使用了 scala 2.10.2/3、SORM 0.38/9,结果相同。这是我的(唯一)实体:

object TapJoy {
  private val SECRET_KEY = "aaa"

  case class AddCreditsRequest(tapJoyId: String, verifier: String, currency: Int, snuid: Long) {
    val created = new DateTime()
   <some methods omitted>
  }
} 

堆栈跟踪:

java.lang.ClassCastException: scala.reflect.internal.Types$TypeRef$$anon$3 cannot be cast to scala.reflect.internal.Symbols$Symbol
at scala.reflect.internal.pickling.UnPickler$Scan.readSymbolRef(UnPickler.scala:788)
at scala.reflect.internal.pickling.UnPickler$Scan.readType(UnPickler.scala:355)
at scala.reflect.internal.pickling.UnPickler$Scan$LazyTypeRef$$anonfun$34.apply(UnPickler.scala:855)
at scala.reflect.internal.pickling.UnPickler$Scan$LazyTypeRef$$anonfun$34.apply(UnPickler.scala:855)
at scala.reflect.internal.pickling.UnPickler$Scan.at(UnPickler.scala:171)
at scala.reflect.internal.pickling.UnPickler$Scan$LazyTypeRef.complete(UnPickler.scala:855)
at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:1229)
at scala.reflect.internal.Types$TypeRef.thisInfo(Types.scala:2399)
at scala.reflect.internal.Types$TypeRef.baseClasses(Types.scala:2404)
at scala.reflect.internal.Types$Type.findMembers(Types.scala:1093)
at scala.reflect.internal.Types$Type.membersBasedOnFlags(Types.scala:718)
at scala.reflect.internal.Types$Type.members(Types.scala:661)
at scala.reflect.internal.Types$Type.members(Types.scala:343)
at sorm.reflection.ScalaApi$TypeApi.members(ScalaApi.scala:11)
at sorm.reflection.ScalaApi$TypeApi.properties(ScalaApi.scala:13)
at sorm.reflection.Reflection.properties(Reflection.scala:31)
at sorm.core.Initialization$.errors(Initialization.scala:29)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.immutable.Set$Set1.foreach(Set.scala:74)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
at sorm.Instance$Initialization.<init>(Instance.scala:212)
at sorm.Instance.<init>(Instance.scala:29)

或者

scala.reflect.internal.Symbols$CyclicReference: illegal cyclic reference involving method productPrefix
at scala.reflect.internal.Symbols$Symbol$$anonfun$info$3.apply(Symbols.scala:1218)
at scala.reflect.internal.Symbols$Symbol$$anonfun$info$3.apply(Symbols.scala:1216)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.reflect.internal.Symbols$Symbol.lock(Symbols.scala:482)
at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:1216)
at scala.reflect.internal.Symbols$Symbol.tpe(Symbols.scala:1200)
at scala.reflect.internal.Symbols$Symbol.tpeHK(Symbols.scala:1201)
at scala.reflect.internal.Types$Type.computeMemberType(Types.scala:784)
at scala.reflect.internal.Symbols$MethodSymbol.typeAsMemberOf(Symbols.scala:2646)
at scala.reflect.internal.Types$Type.memberType(Types.scala:779)
at scala.reflect.internal.Types$Type.findMembers(Types.scala:1117)
at scala.reflect.internal.Types$Type.membersBasedOnFlags(Types.scala:718)
at scala.reflect.internal.Types$Type.members(Types.scala:661)
at scala.reflect.internal.Types$Type.members(Types.scala:343)
at sorm.reflection.ScalaApi$TypeApi.members(ScalaApi.scala:11)
at sorm.reflection.ScalaApi$TypeApi.properties(ScalaApi.scala:13)
at sorm.reflection.Reflection.properties(Reflection.scala:31)
at sorm.core.Initialization$.errors(Initialization.scala:29)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.immutable.Set$Set1.foreach(Set.scala:74)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
at sorm.Instance$Initialization.<init>(Instance.scala:212)
at sorm.Instance.<init>(Instance.scala:29)
4

1 回答 1

2

您如何从运行时构建的外部化配置(例如 typesafe 的 application.conf)中驱动您的 DB/Instance 单例?

很直接。例如:

val db = {
  val config = readAndParseConfigDataSomehow()
  new sorm.Instance( 
    entities = ...,
    url = config.url,
    user = config.url,
    password = config.url,
    poolSize = config.poolSize,
    initMode = config.initMode
  )
}

您如何进行依赖注入(构造函数或蛋糕模式或?)以将相同的代码用于生产/测试数据库..还是完全使用另一种策略?

SORM 确实没有任何特殊要求。您可以传递适合您的实例,就像任何其他值一样,因此使用的实际实例可以依赖于上下文:函数或构造函数参数,例如def doSomeStuffWithDB( db : sorm.Instance ){ ... },如果您是它们的粉丝,甚至是隐式参数(我知道我'不是)。策略的选择完全取决于您。

关于在生产环境和测试环境之间切换 - 您已经提出了一个明显的解决方案 - 配置文件。

于 2013-10-08T00:40:31.297 回答