8

Where should I place a transient domain class in a grails app?

Ie I have an Action class that will be passed about, and used, but never saved. Should this be in the grails-app/domain folder, or somewhere else?

C. Ross
  • 31,137
  • 42
  • 147
  • 238

2 Answers2

15

grails-app/domain is for persistent domain classes, but not all of your application's domain-related classes need to be there, e.g. in this case where you want to use it as a value object. You can put these in src/groovy along with other classes that aren't considered Grails artifacts.

If you want the classes to support validation, you can annotate them with @Validateable - see section "7.5 Validation Non Domain and Command Object Classes" in the ref docs: http://grails.org/doc/latest/

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • Would you use a Command Object if you need the dependency injection and/or data binding aspects, and a @Validateable POGO otherwise? Any other considerations when choosing one approach over the other? – chrislatimer Feb 19 '11 at 17:01
  • 1
    Command objects are convenient, but only usable in a Controller, so often an @Validateable class in src/groovy is more flexible – Burt Beckwith Feb 19 '11 at 17:29
  • Current URL for the docs: http://grails.org/doc/latest/guide/validation.html#validationNonDomainAndCommandObjectClasses – Miscreant Jan 26 '14 at 01:54
1

I think a CommandObject may fit the bill. These typically go in the same directory as your controllers, have the same validation features available to domain objects, but are never persisted. Great for things like search forms.

chrislatimer
  • 3,560
  • 17
  • 19