开发者

"Overloading" standard GORM CRUD methods

开发者 https://www.devze.com 2022-12-10 21:19 出处:网络
Wanna do the following: BootStrap { def init = {servletContext -> ........ MyDomainClass.metaClass.save = {->

Wanna do the following:

BootStrap {
  def init = {servletContext ->
  ........
  MyDomainClass.metaClass.save = {-> 
     delegate.extraSave()
     //////// how to call original save() here?
  }

}
.........
}

P.S. MyDomainClass#extraSave is defined as public void extraSave(){.开发者_运维问答....}


First of all, Bootstrap.groovy may not be the best place to do this kind of metaprogramming. The problem with this approach is that the changes to the classes will be applied when the application starts, but you may lose these changes when the application is reloaded. Obviously this is only an issue during development, and not an issue at all if you don't mind restarting the server every time you make a change, but I'll bet this would quickly become a major annoyance. In order to have the changes applied when app is reloaded as well, you should move the metaprogramming into a plugin, where you can hook into the onChange application lifecycle event.

So the steps are:

  • Create a plugin
  • Do the metaprogramming in the doWithDynamicMethods and onChange closures of the plugin descriptor

Here's a complete example where I "override" the chain() method on all the controller classes. The code to do likewise for the save() method of domain classes should only require some obvious replacements, e.g. use application.domainClasses instead of application.controllerClasses

def doWithDynamicMethods = {ctx ->

    application.controllerClasses.each {controller ->
        replaceChain(controller)
    }
}

def onChange = {event ->
    if (application.isArtefactOfType(ControllerArtefactHandler.TYPE, event.source)) {
        def clz = application.getControllerClass(event.source?.name)
        replaceChain(clz)
    }
}

private replaceChain(controllerClass) {

    // Save a  reference to the grails chain() method
    def grailsChain = controllerClass.metaClass.pickMethod("chain", [Map] as Class[])

    controllerClass.metaClass.chain = {Map params ->

        println "My code to execute before chain goes here"

        // Invoke the grails chain() method
        grailsChain.invoke(delegate, [params] as Object[])

        println "My code to execute after chain goes here"
    }
}


why not leveraging the GORM events for this purpose? In the Domain class:

def extraSave() {
    // ...
}

def beforeInsert = {
    extraSave()
}

def beforeUpdate = {
    extraSave()
}

IMHO this a cleaner approach. Documentation can be found here


Not sure if the following works, but this might be a solution:

MyDomainClass.metaClass.origSave = MyDomainClass.metaClass.save
MyDomainClass.metaClass.save = {-> 
   delegate.extraSave()
   delegate.origSave()
}

Please give me feedbeck if the above worked...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号