Search code examples
grailsgrails-ormtransactionscopegrails-2.0spring-transactions

multiple methods in service of grails


I have created one service in my grails application. in that service 25 methods are there. Some methods are for fetching data and passes to controller and some methods are for applying business logic and others are for database data CRUD operation.

Is it good idea to write multiple methods with different behavior to service? and Do I have to make service transactional?

and what is the use of default method

def serviceMethod() {

    }

?

this method is created when I am creating new Service...


Solution

  • Is it good idea to write multiple methods with different behavior to service?

    Multiple methods in a service is perfectly fine and it makes simply just makes sense if the methods in a service are related to the context.

    Take for example a service called springSecurityService. You would expect the methods contained therein to be related to spring security operations. You wouldn't expect to find a sendMail method there.

    Do I have to make service transactional?

    You should make services transactional if in that service you perform database operation(mostly writes!). When your service is transactional you have the ability to rollback the database operation in case of a failure.

    what is the use of default method

    The default method is just a place holder. Feel free to edit or delete it :D