solid grails apps with verbose groovy

122
MADRID · NOV 21-22 · 2014 Solid Grails Apps with verbose Groovy Alberto Vilches Consultant at Virtual Software @ albertovilches Greach Conference Organizer http ://greachconf.com

Upload: alberto-vilches

Post on 02-Jul-2015

1.589 views

Category:

Technology


7 download

DESCRIPTION

Codemotion 2014 Video de la charla: https://www.youtube.com/watch?v=YwT1ccikv3s Tras 5 años programando con Grails, me he dado cuenta de que lo he estado haciendo mal. Por programar rápido y hacer un código minúsculo, he perdido muchas cosas por el camino que si tenía cuando programaba aplicaciones web con Java. Voy a contar mi experiencia personal de cómo he evolucionado mis desarrollos para que sean fáciles de mantener y de testear. Vuelven los POJOS y el código estático, pero solo para ayudar a poner un poco de oden. Aplicando al extremo la "S" de SOLID y aplicando patrones de diseño, hablaré de una manera (más) de diseñar aplicaciones web con Grails usando Commands, organizando el código de los controllers y clasificando los servicios y sus respuestas con pequeñas clases.

TRANSCRIPT

Page 1: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Solid Grails Apps

with verbose Groovy

Alberto Vilches

Consultant at Virtual Software

@albertovilches

Greach Conference Organizer

http://greachconf.com

Page 2: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Disclaimer

1. I’m not a guru, I’m not trying to teach you

nothing.

2. This is not a talk about something specific,

this is my personal opinion, I can be

wrong.

3. It’s just my way to do the things…. Now.

Tomorrow I can change my mind, again.

Page 3: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

About me

16 years developing, last 5

years with Groovy/Grails

Madrid GUG usual suspect

Greachconf organizer

2 kids, Maclover, gamer and skeptical

Now working at Virtual Software

Page 4: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

About me

And my code sucks!… and probably yours too.

I can’t be the best

but I can be better than yesterday.

This is a talk about my problems, but I’ll show

you solutions.

Page 5: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

IntroductionPART 0/3

Page 6: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

I want to tell you something…

Page 7: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

I want to tell you about…

The

Grails trap

Page 8: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

A trap? Are you sure?

It’s not a trap for everybody

Some people are invulnerable

Page 9: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

A trap? Are you sure?

It’s not a trap for everybody

Some people are invulnerable

It depends how much

you have suffered with

j2Ee

Page 10: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Please, kill me

I was lost

in the f*cking J2EE World

Three tiers application servers, EJB, RMI, CORBA, JMS, JDBC direct access,

really slooow source java code generation, JNDI, huge monolitic applications

full of jar, war and ear, fighting against DTOs, VO, Facade and Service

Locators, and of course XML, XML, XML, I told you about XML? Yes XML!

Page 11: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

And then…

Page 12: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

And then…

Page 13: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

And then…

Page 14: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

And then…

Groovy and Grails

The same shit but you ride them with a magic

flying unicorn with rocket launcher

0 boilerplate!

Working app from scratch in minutes!

Page 15: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

Really new and young

Not much documentation

Not real complex examples (no petstore!)

Page 16: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

“With great power comes great responsibility”Spiderman movie

“La potencia sin control no sirve de nada”Some old TV ad

“Ten cuidao no corras tanto que te vas a dar”My mother

Page 17: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

Software architecture?

Design patterns?

Page 18: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

Software architecture?

Design patterns?

WHO CARES! WE HAVE

ARTIFACTS(remember? no packages at begining!)

Page 19: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

No boilerplate and CoC becomes the

NEW TRUTH

Page 20: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

No boilerplate and CoC becomes the

NEW TRUTH

the world needs to know

it

Page 21: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

“Look, my code is really cool!”

Page 22: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

“Look, my code is really cool!”

Page 23: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

“Look, my code is really cool!”

(In fact, it really really really sucks)

Page 24: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

no architectural patterns needed, just artifacts

Page 25: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

(no architectural patterns needed, just artifacts

+ really easy data access with gorm)

Page 26: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

(no architectural patterns needed, just artifacts

+ really easy data access with gorm)

x (avoiding the “anemic domain model”

results in more business logic in controllers,

services and domain objects)

Page 27: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

(no architectural patterns needed, just artifacts

+ really easy data access with gorm)

x (avoiding the “anemic domain model”

results in more business logic in controllers,

services and domain objects)

^ (Grails infrastructure everywhere)

Page 28: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Grails: the trap explained

(no architectural patterns needed, just artifacts

+ really easy data access with gorm)

x (avoiding the “anemic domain model”

results in more business logic in controllers,

services and domain objects)

^ (Grails infrastructure everywhere)

===================================

Shit, now i’m lost with grails

too

Page 29: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

My new mess (Grails version)

Page 30: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

My new mess (Grails version)

Page 31: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

PART 1/3The theoric and long part,

with patterns and other stuff.

Page 32: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

How I’m solving it

PROBLEM #1

No control about where the business logic lives

Grails hides Spring and Hibernate, offering to

you new infrastructure you spread in artifacts

Page 33: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

How I’m solving it

PROBLEM #1

No control about where the business logic lives

Create a taxonomy and rules

Grails hides Spring and Hibernate, offering to

you new infrastructure you spread in artifacts

Isolate it

Page 34: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

My house, my rules:

Page 35: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

My house, my rules:

1) Only use services

2) Return rich responses (and use enums!)

3) Too much services? create your own

taxonomy

Page 36: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

1) Only use services

2) Return rich responses (use enums!)

Controllers just call services methods.

To avoid logic in controllers, services have to

return rich object responses.

Controllers and views only have to check

responses status calling short methods with

meaningful names (clean code!).

Page 37: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 38: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 39: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 40: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 41: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 42: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 43: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 44: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

The good parts:

You can add more info into response

classes without change all the app.

Easy to test and mocks.

Meaningful actions.

Page 45: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

The good parts:

You can add more info into response

classes without change all the app.

Easy to test and mocks.

Meaningful actions.

The bad parts:

Verbose.

Page 46: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

The good parts:

You can add more info into response

classes without change all the app.

Easy to test and mocks.

Meaningful actions.

The bad parts:

Verbose.

Page 47: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

What about to put logic in domain classes?

Page 48: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

What about to put logic in domain classes?

IT’S

A

TRAP!

Page 49: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

What about to put logic in domain classes?

“Create methods

in your domain classes,

but delegate calls to

service methods.”

Page 50: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 51: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 52: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 53: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

3) Too much services? create your own

taxonomy

My house, my rules:

“Repository” services

“External” services

“Business” services

Page 54: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Repository” services

One responsability: access data

Gorm/HQL/JDBC, grailsApplication.config,

fileSystem

No transactional!

Page 55: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Repository” services, the good parts:

Isolate Grails infrastructure.

Easy to refactor: better rename (only one

place to modify) or change data source.

Easy to mantain: only one place to look.

Page 56: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Repository” services, the good parts:

Isolate Grails infrastructure.

Easy to refactor: better rename (only one

place to modify) or change data source.

Easy to mantain: only one place to look.

The bad parts:

Verbose, specially the wraping service of

grailsApplication.config

Page 57: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Repository” services, the good parts:

Isolate Grails infrastructure.

Easy to refactor: better rename (only one

place to modify) or change data source.

Easy to mantain: only one place to look.

The bad parts:

Verbose, specially the wraping service of

grailsApplication.config

Page 58: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“External” services

One responsability

access to remote servers

Page 59: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“External” services

One responsability

access to remote servers

Web Services (rest/soap), apis, mail servers, …

FTP, SNMP, JMX, XMPP, …

Amazon S3, Facebook, Google, Twitter, …

Page 60: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“External” services, the good parts

Vertical design.

Each integration has one implementation.

Page 61: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“External” services, the good parts

Vertical design.

Each integration has one implementation.

The bad parts

Too simple: keep in mind some integrations

could need design patterns to avoid huge

service classes full of spaghetti code.

Page 62: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

External services, good parts

Vertical design.

Each integration has one implementation.

The bad parts

Too simple: keep in mind some integrations

could need design patterns to avoid huge

service classes full of spaghetti code.

Page 63: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Business” services

One responsability: your business logic

Can be transactional

Orchest all other services

Page 64: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Business” services, the good parts

Isolate business logic without infrastructure.

Page 65: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Business” services, the good parts

Isolate business logic without infrastructure.

The bad parts

Verbose: you always have to pass through

them.

Page 66: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Business” services, the good parts

Isolate business logic without infrastructure.

The bad parts

Verbose: you always have to pass through

them.

Hey, you can break your own rules and call

directly to repositoy services.

Page 67: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

“Business” services, the good parts

Isolate business logic without infrastructure.

The bad parts

Verbose: you always have to pass through

them.

Hey, you can break your own rules and call

directly to repositoy services.

Page 68: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

My rules

Page 69: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

My rules

Controllers call to business services only

Business services call to any kind of service

Repositories can call other repositories only

External services can’t call other services

Page 70: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Page 71: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Create a taxonomy with your own rules

Page 72: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#1 No control about where the

business logic lives

Create a taxonomy with your own rules

But ensure all the

team respect them

Page 73: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

PART 2/3About the views.

Sweet and short!

Page 74: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

How I’m solving it

PROBLEM #2:

Controllers have too much duplicated and

dynamic code with map arguments (buggy!)

Page 75: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

How I’m solving it

PROBLEM #2:

Controllers have too much duplicated and

dynamic code with map arguments (buggy!)

Clean code: hide infrastructure in single

methods (or classes)

Page 76: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#2 Controllers have too much

duplicated and dynamic code

Page 77: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#2 Controllers have too much

duplicated and dynamic code

Page 78: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#2 Controllers have too much

duplicated and dynamic code

Page 79: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#2 Controllers have too much

duplicated and dynamic code

Page 80: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#2 Controllers have too much

duplicated and dynamic code

The good parts

Meaningful actions, easy reading, reusable

Easy to rename model values or view names

Better testingYou don’t have to assert model or view, just assert method calls

Static code, if you like

Page 81: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#2 Controllers have too much

duplicated and dynamic code

The good parts

Meaningful actions, easy reading, reusable

Easy to rename model values or view names

Better testingYou don’t have to assert model or view, just assert method calls

Static code, if you like

The bad parts

Less flexible than using dynamic methods.

Page 82: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

PART 3/3Final part. A lot of

technical knowledge about data binding.

BONUS TRACK: 10 TRICKS!

Page 83: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

PART 3/3Final part. A lot of

technical knowledge about data binding.

Shut up and keep your eyes open!

Page 84: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

How I’m solving it

PROBLEM #3:

What about the input?

Divide input data in minimal commands

Use new Grails 2.3 binding events

And let me show you some tricks :-)

Page 85: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

A new command instance is created, then:

1 Data is bound from request (json/params),

before and after events are called.

2 Dependency Injection.

3 command.validate() is called.

Finally, your action is executed.

Page 86: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

ControllersApi.class

(org.codehaus.groovy.grails.plugins.web.api)

initializeCommandObject method.

DataBindingUtils.class

(org.codehaus.groovy.grails.web.binding)

ValidationSupport.class

(org.codehaus.groovy.grails.web.plugins.support)

Page 87: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #0

Never ever use domain classes as

commands in your actions.

It’s a really bad practice, you are exposing your

datamodel to the world!

Page 88: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #1

You musn’t validate commands in your

actions.

Grails did that for you already!

Maybe database access will be executed twice.

Page 89: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #2

Yes, you can use more than one command

in your actions.

All of them will be bound using the same data

input.

Page 90: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #3

Pay attention when using new data binding

annotations.

The annotations are bound to the command

class at compile time forever. And maybe you

want a different behavior.

Page 91: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #3

Pay attention when using new data binding

annotations.

Are you sure to use that? What happens if you want to use the

command for importing data from a file using other format date?

Page 92: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky!

Use the same field name!

Page 93: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

How it works? QUIZZ TIME!

Page 94: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

email emailOther value bound

[email protected] empty ?

Page 95: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

email emailOther value bound

[email protected] empty empty!

Page 96: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

email emailOther value bound

[email protected] empty empty!

empty [email protected] ?

Page 97: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

email emailOther value bound

[email protected] empty empty!

empty [email protected] empty too!

Page 98: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

email emailOther value bound

[email protected] empty empty!

empty [email protected] empty too!

[email protected] [email protected] ?

Page 99: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

email emailOther value bound

[email protected] empty empty!

empty [email protected] empty too!

[email protected] [email protected] [email protected]

Page 100: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #4 The @BindUsing is tricky

So, be careful…

Page 101: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #5 Default value converters are tricky

Locale.default is used if no request bound to

current thread.

Page 102: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #5 Default value converters are tricky

You have to create another implementation if

you want to use commands without request.

(i.e.: importing from file in a Quartz job)

Page 103: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #6

Use the binding events

Especially, afterBinding event allows you to do

some ad-hoc binding and validations.

Best part: these events are executed before

validate.

Page 104: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #7

Delegate binding events to the commands.

Instead to create a listener for every command,

create a generic listener and delegate the event

call to your command.

Page 105: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #7

Delegate binding events to the commands.

It’s just an idea, you can do it better :-)

Page 106: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #8

Don’t let Grails to load entities for you.

Only you can access to database in your

business logic, not Grails!

Page 107: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #8

Don’t let Grails to load entities for you.

controller/action?productType.id=25

Page 108: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #8

Don’t let Grails to load entities for you.

controller/action?productType.id=25

Page 109: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #8

Don’t let Grails to load entities for you.

Doing that, you have to create a constraint validator to

ensure the ownership of the entity.

Page 110: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #8

Don’t let Grails to load entities for you.

I don’t like because it needs up to 4 db selects to

get the info.

Page 111: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #8

Don’t let Grails to load entities for you.

Alternative way to do it using afterBinding event. With

only 1 select against db!

Page 112: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #9

You can bind whatever you want!

Just call to

DataBindingUtils.bindObjectToInstance()

It will call to all of your events too!

Page 113: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #9

You can bind whatever you want!

All my commands inherits from this class (kind of…)

Page 114: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #10 Use commands as input in your

services

Ensure they don’t

have errors!

Page 115: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

THE END?You wanna more tricks?

2 MORE TRICKS!

Page 116: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #11 Dependency injection starts

AFTER all the events (even the

beforeBinding)

So, if you need services or any other artifact

injected in your commands, just use:

Holders.applicationContext.autowireCapableBeanFactory.

autowireBeanProperties(command,

AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)

Page 117: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #12 Create your own annotations!

Just create a listener and read the command

annotation in the beforeBinding or afterBinding

event.

Page 118: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #12 Create your own annotations!

Annotation example. It just take a Closure as

the only argument.

Page 119: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #12 Create your own annotations!

How to read the annotation and call the

command from a binding listener:

Page 120: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

#3 How Commands works

TRICK #12 Create your own annotations!

Using the annotation from a command. Real

example!

Page 121: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

No more tricks, no more rules!

New Grails 2.3 Data Binding is very powerful!

Use at your own risk and read documentation!

Divide input data in minimal commands.

Combine with previous patterns.

Create your own rules and respect it.

Page 122: Solid Grails apps with verbose Groovy

MADRID · NOV 21-22 · 2014

Questions? Thank you!

@albertovilches

http://albertovilches.com

@greachconf

http://greachconf.com

10-11 April

2015