我想知道是否有人可以分享使用SORM的一些最佳实践,参考在actor系统中使用和自动化测试(单元/功能)。
你好:
似乎当我没有宣布我的"实例"作为一个单例(对象扩展Instance),而是创建一个新的实例,用于注入我的actor进行测试,我使用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)
答案 0 :(得分:2)
如何从运行时构建的外部化配置中驱动DB / Instance单例(例如typesafe的application.conf)?
非常直截了当。 E.g:
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 ){ ... }
,如果你是他们的粉丝,甚至隐含的参数(我知道我不是)。战略的选择取决于你。
关于在生产和测试环境之间切换 - 您已经提出了一个明显的解决方案 - 配置文件。