简体   繁体   English

如何创建自定义函数以返回grails条件方法的关闭

[英]How to create a custom function that returns a closure of grails criteria methods

I'm trying to abstract this top block of code out to look like the bottom block of code. 我正在尝试将顶部的代码块抽象出来,使其看起来像底部的代码块。

if (params.xId) {
    and {
       'in'(aggregateClassReference, hierarchy['x'])
        eq(aggregateIdReference, params.xId as Long)
     }
}
if (params.yId) {
   and {
       'in'(aggregateReference, hierarchy['y'])
        eq(aggregateIdReference, params.yId as Long)
   }
}

... ...

if (params.xId) { belongsToHierarchy('x', params.xId as Long) }
if (params.yId) { belongsToHierarchy('y', params.yId as Long) }

I'm using gorm criteria queries, but I don't want these big chunks of code. 我正在使用gorm条件查询,但是我不希望这些大块代码。 Is there a way to return a closure of these criteria queries in a custom function? 有没有办法在自定义函数中返回这些条件查询的关闭? The problem right now is I put the below chunks of code in 现在的问题是我将以下代码块放入

def criteria = DetachedCriteria.build(...)

Afterwards I do a 之后我做一个

criteria.list(...)

to execute. 执行。 It'd be great to somehow return a closure of just the 以某种方式返回只关闭

 and {
    'in'{...}
    eq {...}
 }

in a custom function within the build, but I haven't been able to figure that out yet. 在构建中的自定义函数中,但我还无法弄清楚。 A bit new to grails. 有点新的。 Any insight to guide me would be much appreciated :) 任何指导我的见解将不胜感激:)

There are a number of ways to do about this. 有许多方法可以做到这一点。 You haven't shown enough context to narrow in on exactly the best solution is to what you are doing but given what is there I can show somethings that might help. 您还没有显示足够的上下文来确定您正在做什么的最佳解决方案,但是鉴于存在的问题,我可以展示一些可能有所帮助的解决方案。

If you wanted to use criteria queries then instead of something like this... 如果您想使用条件查询,请使用类似以下的方法...

def results = SomeDomainClass.withCriteria {
    if (params.xId) {
        and {
            'in'(aggregateClassReference, hierarchy['x'])
            eq(aggregateIdReference, params.xId as Long)
        }
    }
    if (params.yId) {
       and {
           'in'(aggregateReference, hierarchy['y'])
            eq(aggregateIdReference, params.yId as Long)
       }
    }
}

You could do something like this... 你可以做这样的事情...

def results = SomeDomainClass.withCriteria {
    if (params.xId) {
        belongsToHierarchy 'x', params.long('xId'), delegate
    }
    if (params.yId) {
        belongsToHierarchy 'y', params.long('yId'), delegate
    }
}

// ...

// it isn't clear from your example if
// aggregateClassReference and hierarchy are local
// variables in the context where the criteria
// query is being initialized or if they are
// instance variables.  If they are instance variables
// the code below will work.  If they are local
// variables then they might need to be passed as
// arguments into this belongsToHierarchy method...

void belongsToHierarchy(String arg, long id, delegate) {
    def query = {
        // not sure why you had the "and" in your example, but
        // I will leave it here assuming there is a reason...
        and {
            'in' aggregateClassReference, hierarchy[arg]
            eq aggregateIdReference, id
        }
    }
    query.delegate = delegate
    query()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM