29

Fluent Validation Framework - A DSL for validations using fluent interfaces

Embed Size (px)

Citation preview

Page 1: Fluent Validation Framework - A DSL for validations using fluent interfaces
Page 2: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Overallpresenta3on

Introduc3ontoDomainSpecificLanguagesanditstechniques.AswellasshowingFluentValida3onFrameworkconcep3onprocess.

Page 3: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

DomainSpecificLanguage(DSL)isaprogramminglanguagededicatedtosolveproblemsofanspecificdomain

Page 4: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

whichcanbetechnical

 SQL  HQL  CSS

Page 5: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

orbusinessrelated.

 Banking  Manufacturing

 Logis3cs

Page 6: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

ADSLcanbeinternalorexternal.

Page 7: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

ExternalDSLsareself‐contained

Page 8: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

whileinternalDSLsremainontopofotherprogramminglanguages.

Page 9: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Inotherwords,internalDSLsareAPIs.

That's why they're also known as Fluent Interfaces!

Page 10: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

DSLsallowyoutoexpressdomainsclearly.

Page 11: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Forexample,whileabusinessanalystofabankingdomainwouldsay

Transfer 5.000,00 from Client A's cash account to Client B's savings account.

Page 12: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

AcommonJAVAimplementa3oncouldbe

bankService.transfer(5000.00, clientA.getCashAccount(), clientB.getSavingsAccount());

Page 13: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

AnexternalDSLcouldexpressthesameas

transfer 5000 from clientA.cashAccount to clientB.savingsAccount

Page 14: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

AndaninternalDSLinJAVAcouldlooklike

bankService.execute( new Transfer() .of(5000) .from(clientA.cashAccount()) .to(clientB.savingsAccount()));

Page 15: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

It’snotaboutonesinglemethod

publicTransferof(doublevalue);

Page 16: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

butanen3remeaningfulopera3on.

new Transfer() .of(5000) .from(clientA.cashAccount()) .to(clientB.savingsAccount())

Page 17: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

SometechniquestodefineinternalDSLs

Page 18: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

MethodChaining

new Transfer() .of(5000) .from(clientA.cashAccount()) .to(clientB.savingsAccount())

Page 19: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

NestedFunc3on

bankService.transfer(5000, from(clientA.cashAccount()), to(clientB.savingsAcount()));

Page 20: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

ObjectScoping

bankService.execute( new Transfer(){{ of(5000); from(clientA.cashAccount()); to(clientB.savingsAccount()); }});

Page 21: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

FluentValida3onFrameworkisalabprojectapplyingFluentInterfacestothe

domainofcodevalida3on.

Page 22: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Mainideawaschangingvalida3onslike

if(arg0 == null) throw new IllegalArgumentException();

Page 23: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

tosomethinglike

ensure(arg0).isNotNull();

Page 24: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Howtochangeexcep3onmessage?

ensure(arg0).isNotNull().otherwise("arg0 cannot be null"); //doesn't work

Page 25: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Triggermethodattheendoftheexpression

ensure(arg0).isNotNull().now();

ensure(arg0).isNotNull() .otherwise() .throwThis( new IllegalStateException()) .now();

//not cool!

Page 26: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Anotherop3onwascrea3ngtwoentrypoints

ensure(arg0).isNotNull();

analyse(arg0).and().throwThis(new IllegalStateException()).ifNull();

Page 27: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Mixingmethodchainingwithnestedfunc3ons

ensure(myObject).is(inValidState());

analyse(systemReport).and(notifyByEmail()).ifIsNot(operatingWell());

thesolu3onbecameextensible!

Page 28: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

Todaywesaw

 DSLsarecool!  ExternalDSLsarehardtoimplement  FluentInterfacesandInternalDSLsarethesamething  FluentInterfacesworkgreatforextensivelyusedAPIs

Page 29: Fluent Validation Framework - A DSL for validations using fluent interfaces

www.devoxx.com

That’sallfolks!

 projecthXp://fluval.sourceforge.net  bloghXp://blog.danielgazineu.com

 email/[email protected]

 twiXer@danielgazineu