Grails(10)Guide Book Chapter 9 The Service Layer
9. The Service Layer - Reference Documentation
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 son on.
Creating a Service
>grails create-service hellowworld.simple
It will automatically create a service class in grails-app/services/hellowworld/SimpleService.groovy
Default Declarative Transactions
All services are transactional by default, to disable this set the transactional property to false:
class CountryService {
static transactional = false
}
Grails also fully supports Spring's Transactional annotation for cases.
import org.springframework.transaction.annotation.Transactional
class BookService {
@Transactional(readOnly=true)
def listBooks(){}
@Transactional
def updateBook(){}
}
better way
@Transactional
class BookService {
@Transactional (readOnly=true)
def listBooks(){}
def updateBook(){}
def deleteBook(){}
}
Validation Errors and Rollback
A common use case is to rollback a transaction if there are validation errors.
import grails.validation.ValidationException
class AuthorService {
void updateAge(id, int age) {
def author = Author.get(id)
author.age = age
if(!author.validate()){
throw new ValidationException("Author is not valid", author.errors)
}
}
}
import grails.validation.ValidationException
class AuthorController {
def authorService
def updateAge(){
try{
authorService.updateAge(params.id, params.int("age"))
}catch(ValidationException e){
def author = Author.read(params.id)
author.errors = e.errors
render view:"edit", model: [author: author]
}
}
}
9.2 Scoped Services
prototype - A new service is created every time it is injected into another class
request
flash
flow
conversation - in web flows the service will exist for the scope of the conversation.
session
singleton(default)
configure the scope with this code
static scope = "flow"
9.3 Dependency Injection and Services
Dependency Injection Basics
A key aspect of Grails services is the ability to use Spring Framework's dependency injection features.
class BookController {
def bookService
...
}
Dependency Injection and Services
We can inject services in other services with the same technique.
class AuthorService {
def bookService
}
Dependency Injection and domain classes
We can even inject services into domain classes and tag libraries.
class Book{
…
def bookService
def buyBook(){
bookService.buyBook(this)
}
}
9.4 Using Services from Java
We have java interface, all the groovy service.
10. Testing
Automated testing is a key part of Grails. Hence, grails provides many ways to making testing easier from low level unit testing to high level functional tests.
When we create a controller with command we will automatically create a test class for controller
>grails create-controller com.sillycat.simple
grails-app/controllers/com/sillycat/SimpleController.groovy
test/unit/com/sillycat/SimpleControllerTests.groovy
Running Tests
>grails test-app
Targeting Tests
>grails test-app SimpleController
>grails test-app *Controller
>grails test-app com.sillycat.*Controller
>grails test-app com.sillycat.*
run all the tests in a package including sub packages…
>grails test-app com.sillycat.**.*
target particular test methods
>grails test-app SimpleController.testLogin
10.1 Unit Testing
10.1.1 Unit Testing Controllers
10.1.2 Unit Testing Tag Libraries
10.1.3 Unit Testing Domains
10.1.4 Unit Testing Filters
10.1.5 Unit Testing URL Mappings
10.2 Integration Testing
Simulating Request Data
Testing Web Flows
10.3 Functional Testing
Canoo Webtest - http://grails.org/plugin/webtest
G-Func http://grails.org/plugin/functional-test
Geb - http://grails.org/plugin/geb
Selenium-RC - http://grails.org/plugin/selenium-rc
WebDriver - http://grails.org/plugin/webdriver
References:
http://grails.org/doc/latest/guide/index.html
http://grails.org/doc/latest/guide/services.html
http://grails.org/doc/latest/guide/testing.html
Grails(10)Guide Book Chapter 9 The Service Layer
猜你喜欢
转载自sillycat.iteye.com/blog/1758408
今日推荐
周排行