tags:

views:

72

answers:

1

Is there any recommended way to restrict the visibility of a domain in grails?

Normally you you do something like to get some interface for external use:

def productList = Product.list()
withFormat {
  html {[productList:productList]}
  json { render productList as JSON }
  xml { render productList as XML }
  rss { render(feedType:"rss", productList)}
}

which is equal to

SELECT * FROM product

But by default there proerties in a domain that should not be populated. So I need something to say

SELECT id, name, foo1, foo2 FROM product

so only a list of properties is included in the answer.

+1  A: 

You can use a second domain class sort of like a view. The trick is to configure the mapping so it has the same table as the Product class:

class ProductView {

   String name
   Foo foo1
   Foo foo2

   static mapping = {
      table 'product'
   }
}

Then use that in your UI:

def productList = ProductView.list()
withFormat {
  html {[productList:productList]}
  json { render productList as JSON }
  xml { render productList as XML }
  rss { render(feedType:"rss", productList)}
}
Burt Beckwith
Is there any other way like using AOP? Or a plugin allowing that?
fabien7474
ah good idea but kind of hack, because this is not very flexible. what about performance of it, does hibernate support creation of views?
skurt
The performance is the same as any domain class - Hibernate does a select with just the defined fields. You can map a view but it's tricky since it'll try to create the "table" when using update or create-drop.
Burt Beckwith