我正在寫一個服務,不應該保存任何東西。它獲得了一些價值。在數據庫中查找一些東西,然後迴應一個響應。有什麼我應該做的,使服務更快/更少的開銷?此外最好的方式來傳遞它的東西。我通常通過身份證並重新獲得它;這是好還是壞?Grails - 如何通過域對象和使用服務
例如
class DoStuffController {
def ExampleProcessingService
def yesDoIt = {
def lookup = "findme"
def theObject = ExampleThing.findByLookie(lookup)
def lolMap = ExampleProcessingService.doYourThing(theObject.id)
if(lolMap["successBool"]){
theObject.imaString = "Stuff"
theObject.save()
}
[]
}
}
服務
class ExampleProcessingService{
static transactional = true //???????? false? not-a?
def doYourThing = {theID ->
def returnMap = [:]
def myInstance = ExampleThing.get(theID)
if(myInstance.something)returnMap.put "successBool", true
else returnMap.put "successBool", false
return returnMap
}
}
域對象
class ExampleThing {
String imaString
String lookie
static constraints = {
imaString(nullable:true)
}
def getSomething() {
return true
}
}
自舉
import learngrails.*
class BootStrap {
def init = { servletContext ->
def newThing = new ExampleThing(lookie:"findme")
newThing.save()
}
def destroy = {
}
}
傳遞ID和傳遞對象的優點,劣勢還是標準?這種變化是否因爲我的情況不會在服務中保存任何東西?我在做什麼明顯錯誤?你對標題有更好的建議嗎?
我同意@ burt-beckwith,並希望補充說,沒有任何理由將域ID傳遞給服務,只是爲了進行其他查找你的對象,我會傳遞對象本身,除非你有一個非常好的理由不這樣做。 – sbglasius
我接受這個線程似乎已經死了,這個答案確實有幫助。 – Mikey
謝謝伯特!我試圖重現服務中關閉的非交易行爲。但不能。我測試過的示例代碼可以在這裏找到:http://pastebin.com/9L4jzMp3。任何幫助,將不勝感激。 –