Search code examples
grailsgrails-orm

Grails generate-all generates a Service class ( 3.2.3 )


Since new Grails version 3.2.3 the command

generate-all <Domain Class>

generates a service grails.gorm.services.Service named <Domain Class>Service which is an interface

What is the actual implementation of this, which I can edit?


Solution

  • From

    https://github.com/grails/grails-core/issues/11062

    The service is a DataService and it is explained here:

    http://gorm.grails.org/latest/hibernate/manual/index.html#dataServices

    The @Service annotation is an AST transformation that will automatically implement the service for you. You can then obtain the service via Spring autowiring.

    The @Service transformation will look at the the method signatures of the interface and make a best effort to find a way to implement each method.

    In addition, all public methods of the domain class will be automatically wrapped in the appropriate transaction handling.

    What this means is that you can define protected abstract methods that are non-transactional in order to compose logic. For example:

    @Service(Book)
    abstract class BookService  {
    
       protected abstract Book getBook(Serializable id) 
    
       protected abstract Author getAuthor(Serializable id) 
    
       Book updateBook(Serializable id, Serializable authorId) { 
          Book book = getBook(id)
          if(book != null) {
              Author author = getAuthor(authorId)
              if(author == null) {
                  throw new IllegalArgumentException("Author does not exist")
              }
              book.author = author
              book.save()
          }
          return book
       }
    }