我正在寻找组织以下代码的最佳方式
class ComparePrepMgr(factory:IFactoryBuilder) {
val daoact = factory.actorFactory.getActor[DAOSupervisor] //Returns Option[IActorURI]
def prepare(testplan:TestPlan) : Future[UnitofWorkRequest] =
{
for ( dao <- daoact;
testcase <- testplan.testcases; //Returns testcase instance from list type
sourceenvfut = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.sourceenv,false),None))) ;
destenvfut = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destenv ,false),None))) ;
sourceobjfut = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.source,false),None))) ;
destobjfut = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destination ,false),None)));
sourceenv <- sourceenvfut;destenv <- destenvfut; sourceobj <- sourceobjfut; destobj <- destobjfut
)
{
UnitofWorkRequest(testplan.copy(testcases = List()) , testcase, sourceenv.last, destenv.last, sourceobj.last,destobj.last)
}
class ComparePrepMgr(factory:IFactoryBuilder) {
val daoact = factory.actorFactory.getActor[DAOSupervisor]
def prepare(testplan:TestPlan) =
{
for ( testcase <- testplan.testcases;
dao <- daoact
) yield prepareFuture(testplan,dao,testcase)
}
def prepareFuture(testplan:TestPlan,dao:IActorURI,testcase:TestCaseInfo) =
{
for ( sourceenv<- getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.sourceenv,false),None)));
destenv <- getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destenv ,false),None)));
sourceobj <- getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.source,false),None))) ;
destobj <- getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destination ,false),None)))
) yield UnitofWorkRequest(testplan.copy(testcases = List()) , testcase, sourceenv.last, destenv.last, sourceobj.last,destobj.last)
}
}
赞赏使用1.0版本的任何帮助。
答案 0 :(得分:1)
如果我的问题没有正确构建,请接受我的道歉,我看到很多downvoting,我想要实现的是将代码封装在具有Future返回值的一个函数中,另外还试图绕过Future框架,所以看起来最终我能够在承诺的帮助下实现我所期待的目标。这是最终的代码,如果将来有人碰巧在同一条船上
def prepare(testplan:TestPlan) : Future[UnitofWorkRequest] =
{
val unitworkprom = promise[UnitofWorkRequest]
for ( testcase <- testplan.testcases;
dao <- daoact
)
{
val f1 = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.sourceenv,false),None)));
val f2 = getFuture[Vector[EnvironmentInfo]](dao, queryDAO[EnvironmentInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destenv ,false),None)));
val f3 = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.source,false),None))) ;
val f4 = getFuture[Vector[ObjectInfo]](dao, queryDAO[ObjectInfo](new DataQueryExp(ObjectIdEqOp("_id",testcase.destination ,false),None)))
for ( sourceenv <- f1; destenv <- f2; sourceobj <- f3 ; destobj <- f4)
{
unitworkprom.success(UnitofWorkRequest(testplan.copy(testcases = List()) , testcase, sourceenv.last, destenv.last, sourceobj.last,destobj.last))
}
}
unitworkprom.future
}
所以在promise的帮助下,函数会立即返回空的未来,但是为了理解里面的代码,有4个并行调用的期货会得到结果然后使用promise success函数来设置最终的值触发回调。