Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3699800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:08:49+00:00 2026-05-19T05:08:49+00:00

I have two very similar methods in Grails, something like calculate statistics by os

  • 0

I have two very similar methods in Grails, something like “calculate statistics by os” and “calculate statistics by browser” – effectively both prepare some things, then run a similar query on the DB, then do things with the results. The only part where the methods differ is the query they run in the middle of my method –

def summary = c.list {
    eq('browser', Browser.get(1)) // OR eq('os', OS.get(1))
    between('date', dates.start, dates.end)
}

It occurred to me that the ideal way to refactor it would be to pass in the first line of the closure as a method parameter. Like

doStats (Closure query) {
    ...
    def summary = c.list {
        query
        between('date', dates.start, dates.end)
    }
}

I tried this but “query” gets ignored. I tried query() instead but then the query clause is executed where defined, so this doesn’t work either. I suppose I could just pass the whole closure as a parameter but that seems wrong – the query might also get more complicated in future.

Anyone have any better ideas?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T05:08:50+00:00Added an answer on May 19, 2026 at 5:08 am

    You’re using the criteria DSL which might be different than plain groovy closures.

    To do what you’re asking, you can use the method described here –

    http://mrhaki.blogspot.com/2010/06/grails-goodness-refactoring-criteria.html

    and put your query in to private method.

    The more elegant solution for this is to use named queries in grails –

    http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html

    Look at the

      recentPublicationsWithBookInTitle {
           // calls to other named queries…
           recentPublications()
           publicationsWithBookInTitle()
      }
    

    example –

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two extension methods that are very similar. I'd like to remove the
I have two very similar methods: public IQueryable<User> Find(Func<User, bool> exp) { return db.Users.Where(exp);
I have two very similar programs each trying to run two threads OddThread and
I have two very similar specs for two very similar controller actions: VoteUp(int id)
I have an issue where I have two pages that deal with very similar
I have two classes (this is C#) that are very similar except they each
I have two very similar classes that do essentially the same thing. The only
I'm writing some software that targets two versions of very similar hardware which, until
I have two pieces of code which are very similar in that they register
I have two very simple, identical UITableViews in my app that are populated with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.