25
Grails Services

Grails services

Embed Size (px)

Citation preview

Page 1: Grails services

Grails Services

Page 2: Grails services

Agenda● Separation of concern● Controller’s Responsibility● Introduction of Service● Create a Service● Declaratives Transaction● Transactions Rollback and Session● Scoped Services● Dependency Injection and Services● Using Services from java● Injecting a Service

Page 3: Grails services

hello!I am Vijai Kumar Shukl

You can find me at vijay@nextho

Page 4: Grails services

1.Separation of Concern

Page 5: Grails services

“Consider this analogy: Imagine a company where employees are assigned tasks on different nature of work. Consider Employee has following responsibility

● Handle accounting and releasing of check payments

● Take calls from customers for product support

● Handle administrative tasks such as booking flights plus accommodation of executives

● Manage the schedule of the CEO

Problem with this

1. Complicated to John

2. He have to change his frame of mind

3. It’s not easy to replace that employee

Page 6: Grails services

2.Controller’s Responsibility

Page 7: Grails services

Consider a controller in Grails. Inside a controller, we can do the following:● Handle routing logic

● Invoke GORM operations to manipulate data in the database

● Render text and show it to the user.

The real purpose of a controller is to deal with routing logic- which means✘ Receive requests from users

✘ Invoke the most appropriate business logic

✘ Invoke the view to display the result

Page 8: Grails services

3.Service Introduction

Page 9: Grails services

✘ All Business logic should be implemented in service layer.

✘ Grails has default support and handling for the service Layer.

✘ Services in Grails are the place to put the majority of the logic in your application, leaving controllers responsible for handling request flow with redirects and so on.

Page 10: Grails services

4.Creating A Service

Page 11: Grails services

grails create-service [package_name] [service_name]

A service's name ends with the convention Service, other than that a service is a plain Groovy class

package comclass PublicService{}

It will create two files:-

PublicService (Inside service folder)PublicServiceSpec (Inside test folder)

grails create-service com Public

def publicService

Page 12: Grails services

5.Declarative Transactions

Page 13: Grails services

Services are typically involved with coordinating logic between domain classes, and hence often involved with persistence that spans large operations.The nature of services, they frequently require transactional behaviour. All services are transactional by default. To disable this set the transactional property to false

Note*:- Dependency is the only way that declarative transactions work. You will not get transactional behaviour if you use new operator such as new PublicService()The result is that all methods are wrapped in a transaction and automatic rollback occurs if a method throws a runtime exception (i.e. one that extends RuntimeException) or an Error.

class PublicService{static transactional = false}

Page 14: Grails services

Custom Transaction ConfigurationGrails also provides @Transactional and @NotTransactional annotations for cases:-

x Where you need more fine-grained control over transactions at a per-method levelx Need to specify an alternative propagation level. x For example, the @NotTransactional annotation can be used to mark a particular

method to be skipped when a class is annotated with @Transactional.

class BookService{@Transactional(readOnly=true)

def listBook(){Book.list()

}}

Page 15: Grails services

6.Transactions Management

Page 16: Grails services

✘ Transaction is a very important concept. Hope you are well aware of ACID(Atomicity, Consistency, Isolation, Durability) properties .

✘ Usually we wish certain sequence of database changes to be successful. If not possible we want no operation to happen at all.

✘ Imagine a code that deducts money from one account (accountFrom.balance = accountFrom.balance - amount), and adds money to another account (accountTo.balance = accountTo.balance + amount). Imagine if something happened (an Exception) after deducting from the source account and the destination account was not updated. Money will be lost and not accounted for. For this type of codes, we want an "all or nothing" behavior. This concept is also called atomicity.

✘ Since Grails supports transactions, it automatically do these things to us when we declare a service to be transactional:

If all db operations are successful, reflect the changes to the database (this is also called commit) • If one db operation result in exception, return to the original state and forget/undo all the previous

operations (this is also called rollback) •✘ By default all services are transactional, if you don’t want transactional behavior of the

service you can get rid of it by class TestService(){ static transactional=false }

Page 17: Grails services

✘ When a transaction is rolled back the Hibernate Session used by GORM is cleared. ✘ Means an object within the session become detached and accessing uninitialized lazy-

loaded collections will lead to LazyInitiallizationExceptions.

Page 18: Grails services

7.Scoped Services

Page 19: Grails services

By default, access to service methods is not synchronized, so nothing prevents concurrent execution of those methods. In fact, because the service is a singleton and may be used concurrently, you should be very careful about storing state in a service.You can change this behaviour by placing a service in a particular scope. The supported scopes are:

● prototype - A new service is created every time it is injected into another class

● request - A new service will be created per request

● flash - A new service will be created for the current and next request only

● flow - In web flows the service will exist for the scope of the flow

● conversation - In web flows the service will exist for the scope of the conversation. ie a root flow and its sub flows

● session - A service is created for the scope of a user session

● singleton (default) - Only one instance of the service ever exists

Page 20: Grails services

8.Dependency Injection

Page 21: Grails services

A key aspect of Grails services is the ability to use Spring Framework's dependency injection features.Grails supports "dependency injection by convention".you can use the property name representation of the class name of a service to automatically inject them into controllers, tag libraries, and so on.

class BookController{def bookService{

class BookController{BookService bookService{

class AuthorService{def bookService}

Page 22: Grails services

Our process is easy

create inject use

Page 23: Grails services

def appContext = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)def scrapeScoreService = appContext.scrapeScoreService

Page 24: Grails services

References

http://grails.asia/grails-tutorial-for-beginners-grails-service-layer/

http://docs.grails.org/2.4.4/guide/services.html

Page 25: Grails services

thanks!Any questions?

You can find me [email protected]