I am new to grails and need some input on the patt...
# questions
u
I am new to grails and need some input on the pattern. I have a domain model which slightly varies from the input request I receive. So I am using the
Command
object to validate the incoming request, and later bind and transform the actual domain model. I have also recently came across
gorm listener
so now my question is what is the right place to validate the changes? Is it
Command
or the
Gorm Listener
s
2 separate things. You should validate the Command object and then validate the Domain object using constraints, as 1 is the data coming in and the otehr is the data you are going to save. As you said they are different. Gorm calls validate implicitly when you call save on a domain but you can validate it yourself first.
u
Thank you very much @Steve Osguthorpe But I am still confused why/when would I use the
Gorm Event Listener
? Is it to transform something before writing? or is it to generate some metadata. Sorry if I am being pedantic, but I would like to know the best use-cases on when to use it and when not to
s
I would say that you would use those things to maybe set a derrived property, like " full name" by concatenating "fristnam and surname" or use the asynchronous variants for side effects like triggering an email on user creation. Validation is a synchronous thing and IMO it's better to keep the contrains and validation maethods contain in the object.
partygrails 1
The tooling provides these methods to make the best decisions for your software. There isn't a right or wrong answer really. You should use your judgement.