Grails Anti-Pattern: Everything is a Service

The context

Grails makes it very easy to put any logic of your application in a service. Just grails create-service and you’re good to go. There’s a single instance by default, injectable anywhere. Powerful stuff and makes it easy to get up ‘n running very fast!

Creating a new application, following so-called “best practices” from blogs like these 🙂 and the ‘idiomatic Grails-way’ described in the docs and in tutorials work in the beginning, but there’s always a tipping point — where the application has grown a reasonable size — where one should start following a different, maybe less-Grailsey, strategy.

So what can go wrong by creating services in your application?

Continue reading “Grails Anti-Pattern: Everything is a Service”

Debugging Grails 3, Spring Boot or Gradle with Eclipse

This is a small post for those who’s primary IDE is Eclipse for developing Grails 3 apps or plugins — and are still wondering how to debug their application.

First of all, to clarify: there’s no “Grails” debugging, it’s just Spring Boot and Gradle these days, with Grails 3. Any way to debug these kind of applications allows you to debug a Grails 3 application.

There’s lots of SO posts and Google hits about this subject, so I’m creating this post also as a short “simplest solution” summary for myself and for others.
Continue reading “Debugging Grails 3, Spring Boot or Gradle with Eclipse”

Grails Expert Tip #8 – There’s a Bean for That

grails-expert-tipRecently I code reviewed some of the Grails (2.4.5) code of another team (of course :-)) and here’s a snippet I came across.

In some “search helper service”:

 public int countListWithoutFetching(Collection collection) {
   def session = grails.util.Holders.grailsApplication.mainContext.sessionFactory.currentSession
   return ((Integer) session.createFilter(collection, "select count(*)" ).list().get(0)).intValue()
 }

This team somehow needed some ‘generic’ way of counting the amount of records in the database, I thought.

Continue reading “Grails Expert Tip #8 – There’s a Bean for That”

Quicktip! Grails scaffolding: enums and i18n

Grails scaffolding feature is a great asset for quickly generating create/read/update/delete (CRUD) screens for your domain classes. Create an entity, create a controller and put static scaffold on top. For various types of properties within your domain class a suitable (HTML) input is used, to take the user’s input: booleans become checkboxes, associations with other domain classes become (depending on ownership and constraints) e.g. dropdowns or multi-selects, etc.

Even an enum works out of the box as one could imagine 🙂 This post serves as a small quicktip – primarily for me and other developers looking for these kind of things – of internationalizing scaffolded screens in general and enums in particular using  toString() and Spring’s MessageSourceResolvable.

Take the following Product with a Status:

class Product {

	enum Status {
		AVAILABLE, SOLD_OUT
	}

	String name
	Status status

	static constraints = { name blank: false, unique: true }
}

class ProductController {

	def scaffold = true
}

Opening up our /product/create url will show what you would expect.

Continue reading “Quicktip! Grails scaffolding: enums and i18n”