gpt4 book ai didi

grails - 使用 Grails 域查找方法重复代码

转载 作者:行者123 更新时间:2023-12-02 14:42:31 25 4
gpt4 key购买 nike

最初的问题

如果您有不同的方法,基本上只有一行不同,是否有一种方法可以通过创建一种方法使其干燥。

例子:

def showA( ) {
def instance

try {
instance = A.findById( params.id )
} catch ( Exception e ) {
def message = "Error while retrieving details for the given id ${ params.id }, $e"
log.error message
responseAsJson( 400, "Invalid id", message )
return false
}

return checkAndRender(instance, params.id);
}

def showB( ) {

def instance

try {
instance = B.findByBId( params.BId )
} catch ( Exception e ) {
def message = "Error while retrieving details for the given id ${ params.id }, $e"
log.error message
responseAsJson( 400, "Invalid id", message )
return false
}

return checkAndRender(instance, params.id);
}

那么,是否有一种方法可以制作一个方法并简单地作为参数传递:
  • 域类
  • 要搜索的 ID

  • 还是改为传递 SQL 语句会更好?

    更新

    根据@dmahapatro 评论,我提出以下建议:
    def showA( ) {
    def clos = {id -> A.findByAId( id ) }
    return findAndShow(clos, params.AId, params )
    }

    def showB( ) {
    def clos = {id -> B.findByBId( id ) }
    return findAndShow(clos, params.BId, params )
    }

    def findAndShow(Closure closure, def id, def p)
    {
    def instance
    try {
    instance = closure(id)
    }
    catch ( Exception e ) {
    def message = "Error while retrieving instance details for the given id ${ id }, $e"
    log.error message
    responseAsJson( 400, "Invalid Id", message )
    return false
    }

    return checkAndRender(instance, id);
    }

    剩下的问题只有:
  • 如何进一步清理/使其更清洁。
  • 如何绕过警告:

    The [findAndShow] action in [ApiController] accepts a parameter of type [groovy.lang.Closure]. Interface types and abstract class types are not supported as command objects. This parameter will be ignored.

       def findAndShow(Closure closure, def id, def p)
  • 最佳答案

    如果你想要一个 DRY 代码,你应该担心的第一件事是定义更好的异常处理。尝试在任何地方捕获代码以处理对客户端的响应并不是很干燥,如果将数据访问代码放在服务中,则可以从它们中抛出异常并使用全局 Controller 来捕获错误并处理响应。例如:

    class ErrorController {

    def serverError() {
    if (request.format == 'json') {
    //Code for handling errors in json request, request.exception stores the data about the exception.
    } else {
    //Code for handling errors in non-json request, e.g:
    render(view: 'error', model: [msg: 'Something went wrong']) //add an error view for this
    }
    }
    }

    如果您愿意,还可以为其他类型的错误(403、404 等)添加处理程序

    添加到 UrlMappings.groovy
        "500"(controller: "error", action: "serverError")

    现在您可以使用新的错误处理和反射重构代码:

    Controller :
       class MyController {

    def myService

    def show() {
    def result = myService.myFind(params.className,params.id)
    render result as JSON //Render stuff
    }
    }

    服务:
           import grails.util.Holders

    class MyService {

    def myFind(String className, Long id) {
    def result = Holders.getGrailsApplication().getDomainClass('com.mypack.'+ className).findById(id)
    if(!result) {
    throw new ServiceException('really descriptive and usefull error msg')
    }
    }
    }

    我定义了一个 ServiceException 类,因此我可以使用 instanceOf 运算符在我的 ErrorController 中为其添加自定义逻辑。

    关于grails - 使用 Grails 域查找方法重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27272126/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com