Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails GORM best practices

Does anyone know if there is a definitive best practices guide for GORM? I find information scattered across different blogs and different resources, but I can't find a definitive guide. I find stuff that says to not do database related stuff in controllers and to keep those things in the service layer, for example. However, it'd just be nice to se what the suggested approach for writing a simple web app is. Should we always use command objects in controllers and pass those command objects to services? Should we store those command objects in session rather than storing actual domain objects in session, which seems to cause a lot of lazy init exceptions, etc.?

I've tried to piece together the information that I've found, but if anyone knows of a comprehensive resource, that would be great.

like image 357
cloudwalker Avatar asked May 13 '14 13:05

cloudwalker


2 Answers

There is some great information available from the GORM Gotchas series. It's in three parts.

GORM Gotchas (Part 1)

GORM Gotchas (Part 2)

GORM Gotchas (Part 3)

To answer your specific questions about Services and Command objects.

Q: "Should we always use command objects and Services?"

A: Some would argue that it's overkill to do so, however I personally think it's a great pattern and makes things much easier to test and extend. It may seem like a lot of effort but it does pay off in large projects.

Q: "Should we store command objects in session rather than domain objects?"

A: Store as little in the session as possible (if at all). If you have to store something there it's best that it be small and light weight. Command objects (typically) are going to be better for this than a Domain class.

Update (11/19/2014)

I'd like to highlight a very good series that outlines a lot of the potential issues faced using GORM and Hibernate. It's very long, but worth reading if you plan on using GORM/Hibernate in a large scale multi-user project. Don't be turned away by the negative approach because it does contain a lot of useful information.

I don't like Hibernate (and Grails), PART 1

I don't like Hibernate/Grails, part 2, repeatable finder problem: trust in nothing!

I don't like Grails/Hibernate part 3. DuplicateKeyException: Catch it if you can.

I don't like Grails/Hibernate, part 4. Hibernate proxy objects.

I don't like Hibernate/Grails part 5: auto-saving and auto-flushing

I don't like Hibernate/Grails part 6, how to save objects using refresh()

I don't like Hibernate/Grails part 7: working on more complex project

I don't like Hibernate/Grails, part 8, but some like Hibernate and Grails. Why?

I don't like Hibernate/Grails part 9: Testable code

I don't like Hibernate/Grails part 10: Repeatable finder, lessons learned

I don't like Hibernate/Grails part 11. Final thoughts.

like image 172
Joshua Moore Avatar answered Sep 21 '22 12:09

Joshua Moore


The book Grails in Action talks a lot about best practices in Grails. At the time of this writing it isn't published in its final form, but you can buy and read the preview.

I was recently looking for the same answers you are asking and that book has helped me a lot.

like image 37
wwwclaes Avatar answered Sep 19 '22 12:09

wwwclaes