Fluent Validation Framework - A DSL for validations using fluent interfaces

Preview:

Citation preview

www.devoxx.com

Overallpresenta3on

Introduc3ontoDomainSpecificLanguagesanditstechniques.AswellasshowingFluentValida3onFrameworkconcep3onprocess.

www.devoxx.com

DomainSpecificLanguage(DSL)isaprogramminglanguagededicatedtosolveproblemsofanspecificdomain

www.devoxx.com

whichcanbetechnical

 SQL  HQL  CSS

www.devoxx.com

orbusinessrelated.

 Banking  Manufacturing

 Logis3cs

www.devoxx.com

ADSLcanbeinternalorexternal.

www.devoxx.com

ExternalDSLsareself‐contained

www.devoxx.com

whileinternalDSLsremainontopofotherprogramminglanguages.

www.devoxx.com

Inotherwords,internalDSLsareAPIs.

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

www.devoxx.com

DSLsallowyoutoexpressdomainsclearly.

www.devoxx.com

Forexample,whileabusinessanalystofabankingdomainwouldsay

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

www.devoxx.com

AcommonJAVAimplementa3oncouldbe

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

www.devoxx.com

AnexternalDSLcouldexpressthesameas

transfer 5000 from clientA.cashAccount to clientB.savingsAccount

www.devoxx.com

AndaninternalDSLinJAVAcouldlooklike

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

www.devoxx.com

It’snotaboutonesinglemethod

publicTransferof(doublevalue);

www.devoxx.com

butanen3remeaningfulopera3on.

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

www.devoxx.com

SometechniquestodefineinternalDSLs

www.devoxx.com

MethodChaining

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

www.devoxx.com

NestedFunc3on

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

www.devoxx.com

ObjectScoping

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

www.devoxx.com

FluentValida3onFrameworkisalabprojectapplyingFluentInterfacestothe

domainofcodevalida3on.

www.devoxx.com

Mainideawaschangingvalida3onslike

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

www.devoxx.com

tosomethinglike

ensure(arg0).isNotNull();

www.devoxx.com

Howtochangeexcep3onmessage?

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

www.devoxx.com

Triggermethodattheendoftheexpression

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

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

//not cool!

www.devoxx.com

Anotherop3onwascrea3ngtwoentrypoints

ensure(arg0).isNotNull();

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

www.devoxx.com

Mixingmethodchainingwithnestedfunc3ons

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

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

thesolu3onbecameextensible!

www.devoxx.com

Todaywesaw

 DSLsarecool!  ExternalDSLsarehardtoimplement  FluentInterfacesandInternalDSLsarethesamething  FluentInterfacesworkgreatforextensivelyusedAPIs

www.devoxx.com

That’sallfolks!

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

 email/gtalkdanielgazineu@gmail.com

 twiXer@danielgazineu