0

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...

sanghavi7
  • 758
  • 1
  • 15
  • 38

2 Answers2

3

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

gotomanners
  • 7,808
  • 1
  • 24
  • 39
  • thanx @gotomanners, and One thing want to ask that Is it good idea to make common service for saving all object ? means calling one method which is defined in service for storing my object? – sanghavi7 Jun 13 '12 at 13:08
2

Services are transactional by default in Grails - http://grails.org/doc/latest/guide/services.html#declarativeTransactions

The serviceMethod is generated by the CreateService script. It is just an example, so you can delete it if you wish.

You could put all your business logic in services. It is convenient, but note that this is more of an anti-pattern called Anaemic Domain Model

imho, you should try to follow object-oriented principles and put most of your business processes in the domain classes. When there is a complex process involving multiple domain object, then maybe you can put this in a service.

aldrin
  • 4,482
  • 1
  • 33
  • 50