grails 中保存和更新方法的习惯用法
grails 中是否有任何习惯用法可以帮助我们保存域对象?
例如
我可能想做类似的事情
if(candidate.hasErrors || !candidate.save)
{
candidate.errors.each {
log it
}
但是我不想将逻辑传播到我执行domainObject.save 的所有地方。
我也不想要像存储库这样的单独的类,我将这个domainObject传递给它并放入这个逻辑
谢谢 苏达山
Are there in any idioms in grails which help us with saving domain objects ?
For example
i may want to do something like
if(candidate.hasErrors || !candidate.save)
{
candidate.errors.each {
log it
}
However i do not want to spread the logic across all the places i do domainObject.save.
I also do not want seperate class like say repo to which I pass this domainObject and put in this logic
Thanks
Sudarshan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我用来验证和保存的服务方法,但会在失败时记录已解析的验证消息。使用它而不是仅仅使用
println error
或log.warn error
会很有帮助,因为错误对象的 toString() 非常冗长,你只想看看会显示什么关于 GSP:这是控制器中的一个示例:
编辑:也可以将这些方法添加到 MetaClass,例如在 BootStrap.groovy 中:
这取决于范围内的“log”变量,这在任何 Grails 中都是如此人工制品。这会稍微改变控制器的用法:
作为元类方法,重命名它可能更有意义,例如
saveWithWarnings()
。Here's a service method that I've used to validate and save, but log resolved validation messages on failure. It's helpful to use this instead of just
println error
orlog.warn error
since the toString() for error objects is very verbose and you just want to see what would be displayed on the GSP:And here's an example in a controller:
Edit: It's also possible to add these methods to the MetaClass, e.g. in BootStrap.groovy:
This depends on a 'log' variable being in scope, which will be true in any Grails artifact. This changes the controller usage slightly:
As a metaclass method it may make more sense to rename it, e.g.
saveWithWarnings()
.