I'm trying to nail down what the difference is between these three folders and what I should be putting in them.
As of right now I have been throwing class, interfaces, and anything else that directly relates to the structure of my domain classes (by extending or implementing) into the src
folder. Anything that involves additional transactional logic beyond what a Grails controller does by default I have been putting into the grails-app/services
folder. Lastly, any class that contains "helper" methods (i.e. comparing various things, special string operations, etc.) I have been putting into the grails-app/utils
folder.
If I have missed the mark for what these folders should be used for please put me on the right path.
That's pretty close. grails-app/utils is for Codec classes - it's weirdly named and underdocumented. I'd move the helper classes back to src/groovy.
Using services to do transactional work is great, but you can use services for non-transactional methods too. Add static transactional = false
to the service classes that have utility methods that don't need transactions. Note that there's no transactionality in controllers, so you should move all persistence to transactional services.
Static utility methods in a src/groovy helper class and non-transactional methods in a service are pretty much equivalent, so for me the deciding which route to take would come down to dependencies. If the class depends on Spring beans, make it a service and reference them via dependency injection. Otherwise just make it a helper class.
If you find yourself adding a method to a domain class that has dependencies on non-domain classes, ask yourself why this method could be in a service instead. This leads to having a corresponding service for almost every domain class.
This makes it simpler to put your domain classes in a plugin so that you can extend functionality by building several apps using the same domain model rather than creating an unmaintainable big ball of mud. It is also consistent with the application model described by Uncle Bob Martin in the Lost Years of Architecture http://www.youtube.com/watch?v=WpkDN78P884
Given that you are thinking about Architecture it would be a good idea to read some of the work of Uncle Bob and of Martin Fowler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With