我是Scala和Play的新手,并编写了一个包含业务和展示逻辑的“do all”控制器。我想将业务逻辑重构到控制器之外。
这是我的Scala/Play代码。有什么好的/惯用的方法可以将业务逻辑从控制器中重构出来,并提供清晰的接口?
object NodeRender extends Controller {
...
def deleteNode(nodeId: Long) = Action { request =>
//business logic
val commitDocument = Json.toJson(
Map(
"delete" -> Seq( Map( "id" -> toJson( nodeId)))
))
val commitSend = Json.stringify( commitDocument)
val commitParams = Map( "commit" -> "true", "wt" -> "json")
val headers = Map( "Content-type" -> "application/json")
val sol = host( "127.0.0.1", 8080)
val updateReq = sol / "solr-store" / "collection1" / "update" / "json" <<?
commitParams <:< headers << commitSend
val commitResponse = Http( updateReq)()
//presentation logic
Redirect( routes.NodeRender.listNodes)
}
在Python/Django中,我编写了两个类
XApiHandler
和XBackend
,并在它们之间使用了一个干净的接口。xb = XBackend( user).do_stuff()
if not xb:
return a_404_error
else:
return the_right_stuff( xb.content) #please dont assume its a view!
deleteNode
操作? - EECOLOR