views:

372

answers:

2

Let's say I have a domain class called ShopCategoryPageTab. And I have a domain class called Product.

I want ShopCategoryPageTab to have a list of Products. However, this list is not static, but determined by a formula.

For example, I might want to have a "products" property which would list all products with criteria X, Y Z.

So this property/list is not entered manually by someone, it is dynamically generated. (products can be removed/added by an external applications, product properties could be changing).

Is there such a thing?

+2  A: 

Often you call the dynamically added methods like list(), findAll(), findByFooAndBar(), etc., but you can always add your own. So in your case you'd create something like this:

class ShopCategoryPageTab {

   ...
   List findAllProductsByXYZ(x, y, z) {
      ...
   }
}

Name it however you like of course, and the implementation will be a criteria query or an HQL query via executeQuery().

Burt Beckwith
You could implement this using the "named query" feature introduced in Grails 1.2http://www.grails.org/1.2+Release+Notes
Don
A: 

Agree with @Burt above.. alternatively you can also call your methods as getXProduct() or getYProduct() and access them as fields instead of methods.

The fields would be like XProduct and YProduct. You'll have to mark these fields transients explicitly..

Hope it helps..

Satya