Thursday 17 April 2008

Grails course in Asturias, Spain

I'm pleased to annonce a Grails course in Asturias, Spain. The course is organized by the College of Engineers of Principado de Asturias, from 5th to 10th of may, around 15 hours training in Groovy and Grails technologies.

The title of course can be translate as "Agile Development in Java EE with Grails". If any one is interested, can contact to me by mail.

Here the slides (in Spanish) for my first class.

Monday 7 April 2008

Grails: Plugglabe Service, Reloaded

Yesterday I've read, the post from Josh Creed about a pluggable service in Grails and I've been thinking the way to do with IoC and wire provider with Spring.

Let's do it, with small test.

First my service class GreetService:
class GreetService {

def provider

def sayHello(String name){
provider.sayHello(name)
}

}
Now, two Providers:
class DevProvider {
def sayHello(String name){
println "DevProvider: hello ${name}"
}
}


class TestProvider {
def sayHello(String name){
println "TestProvider: hello ${name}"
}
}
Now I'm gonna use Spring DSL, in resources.groovy, to define provider bean that depends of environment:
import grails.util.*
beans = {
switch(GrailsUtil.environment) {
case "test":
provider(TestProvider)
break
case "development":
provider(DevProvider)
break
}
}
And that's all with Groovy and Grails magic, here the test:

class GreetServiceTests extends GroovyTestCase {

def greetService

void testGreet() {

greetService.sayHello("World!!!!!!")
}
}
// output: TestProvider: hello World!!!!!!
And in Grails console, another test:

def service = ctx.getBean("greetService")
service.sayHello("world")

// output: DevProvider: hello world