Scala io2013 : Our journey from UML/MDD to Scala macros

31
Our journey from UML/MDD to Scala macros Hayssam Saleh

description

My talk @ SacalIO_FR 2013 about the Slick Macros project

Transcript of Scala io2013 : Our journey from UML/MDD to Scala macros

Page 1: Scala io2013 : Our journey from UML/MDD to Scala macros

Our journey from UML/MDD to Scala

macrosHayssam Saleh

Page 2: Scala io2013 : Our journey from UML/MDD to Scala macros

Summary

• What this talk is about ?o Why did we choose UML ?o Why did we move from UML to DSLo Our experience in designing a DSL on top of Slick using

Scala macros.

• The macro-based DSL on top of Slick

• Implementationo How macros worko @Model annotation-macro o Dynamic methods statically compiled

Page 3: Scala io2013 : Our journey from UML/MDD to Scala macros

Why did we move from UML to scala macros ?

Page 4: Scala io2013 : Our journey from UML/MDD to Scala macros

Why UML ?

• Encapsulationo Hide implementation details and expose relevant

abstractions only

• Communicationo Product owner and dev team understand each other

• Qualityo Boilerplate code is generated with respect to the

architect guidelines. Dev team focus on business rules

Page 5: Scala io2013 : Our journey from UML/MDD to Scala macros

Why not UML ?

• Lack of efficiencyo Any modification require code regeneration

UML to XMI (20 % - more or less) XMI to code (78% - more or more) Code to bytecode (2% - much less)

o Excessive generation time Code is regenerated for all model entities regardless of

whether they were modifiedo Almost inexistent (real life) collaborative capabilities

Always locked model syndrome Anyone tried to merge UML models on a regular basis ?

• Impedance mismatcho Not all concepts are easily represented in UML

Page 6: Scala io2013 : Our journey from UML/MDD to Scala macros

Why Scala macros ?

Page 7: Scala io2013 : Our journey from UML/MDD to Scala macros

Why not Scala macros ?

• DSL design is complexo We are used to apply existing patternso Are we used to design grammars ?

• Difficult to develop and maintaino Development at the meta-levelo Hey, we’re working on the AST

Page 8: Scala io2013 : Our journey from UML/MDD to Scala macros

The DSL

Page 9: Scala io2013 : Our journey from UML/MDD to Scala macros

The DSL Goal

• Allow the product owner and the developer o to share info about the static representation of the

system (The database model in our case)

• The product owner should be able to read it

• The developer should be able to compile it

Page 10: Scala io2013 : Our journey from UML/MDD to Scala macros

The good old architecture

http://www.mywebsite.com

ControllerController

Pe

rsisten

ce

laye

rP

ersiste

nce

la

yer

Model

ServiceService

DAODAO

ModelModel

DAODAO

ModelModel

Page 11: Scala io2013 : Our journey from UML/MDD to Scala macros

Why Slick as the persistence framework

• Why Slick as a persistence frameworko Because it ’s not an ORM so we’ve got

DBA is happy : No more huge SQL requests that makes SQL auditing difficult

Network is happy : No unnecessary round trip Webserver is happy : no more objects to proxify

€€€ When the customer is happy my boss is happy too

Page 12: Scala io2013 : Our journey from UML/MDD to Scala macros

Why Slick as the persistence framework

• What we losto Automatic detection of columns to update

Who cares ?• Unit of I/O defaults to 8K for Postgres, Oracle, SQLServer …

So for most cases (to say the least), updating the whole object has no real impact on performance

o Automatic inserts of dependent objects Our use case focus is on scalable OLTP applications Is it really an issue in OLTP ?

• Do we really want the framework to guess what we’re doing ?• Does it justify the overhead ?

We’ve got to rethink our persistence patterns• We’ve got actors• We’ve got transactions• We’ve got joins

Page 13: Scala io2013 : Our journey from UML/MDD to Scala macros

A Slick-macros example

Embedded valueEmbedded value

1..1 relationship1..1 relationship

0..1 relationship0..1 relationship

*..* relationship*..* relationshipConstraintsConstraints

Timestamp all rowsTimestamp all rows

Page 14: Scala io2013 : Our journey from UML/MDD to Scala macros

The equivalent Slick Code 1/4

Page 15: Scala io2013 : Our journey from UML/MDD to Scala macros

The equivalent Slick Code 2/4

Page 16: Scala io2013 : Our journey from UML/MDD to Scala macros

The equivalent Slick Code 3/4

Page 17: Scala io2013 : Our journey from UML/MDD to Scala macros

The equivalent Slick Code 4/4

Page 18: Scala io2013 : Our journey from UML/MDD to Scala macros

Our UML Model

Page 19: Scala io2013 : Our journey from UML/MDD to Scala macros

Slick Mapping generated by the macro

Page 20: Scala io2013 : Our journey from UML/MDD to Scala macros

Implementation

Page 21: Scala io2013 : Our journey from UML/MDD to Scala macros

Where did the Boilerplate code goes ?

Scala source codeScala source code

Abstract Syntax TreeAbstract Syntax Tree

Java Byte CodeJava Byte Code

RuntimeRuntime

Boilerplate XML / Scala / …

Boilerplate XML / Scala / …

Annotations

Boilerplate code

Page 22: Scala io2013 : Our journey from UML/MDD to Scala macros

Outputting the Trees

AST nodes have extractors that definitely help

Page 23: Scala io2013 : Our journey from UML/MDD to Scala macros

Dynamic methods Statically compiled

Part 2

Page 24: Scala io2013 : Our journey from UML/MDD to Scala macros

Simplify Querying

• Simplify finderso Instead of this

o Write this (pseudo-code)

def macrosdef macros

Page 25: Scala io2013 : Our journey from UML/MDD to Scala macros

Simplify Querying

• Simplify updateo Instead of this

o Write this

def macrodef macro Argument names and types are known at call site onlyand still this code remains typechecked at compile timeArgument names and types are known at call site onlyand still this code remains typechecked at compile time

Page 26: Scala io2013 : Our journey from UML/MDD to Scala macros

Implementation

Page 27: Scala io2013 : Our journey from UML/MDD to Scala macros

A mix of Dynamic and Scala-macros

• First the Slick Query object gets the Dynamic trait

• Since arguments are all named we define the applyDynamicNamed method as a macroo Def macros are applied at call site, we can thus

typecheck at call site against the prefix object.

Page 28: Scala io2013 : Our journey from UML/MDD to Scala macros

Conclusion

• As a developero It was almost

A copy/paste task and AST node substitutiono Made easier using the quasiquote feature

• As a usero Much less code to maintaino Reduced time to delivero No runtime overhead

Page 29: Scala io2013 : Our journey from UML/MDD to Scala macros

Source code

• Slick-macros @hayssams : http://www.github.com/ebiznext/slick-macros

Page 30: Scala io2013 : Our journey from UML/MDD to Scala macros

References

• Scala macros @xeno_by : http://docs.scala-lang.org/overviews/macros/usecases.html

• Slick @szeiger : http://slick.typesafe.com/docs/#talks

• Another source about macros you may find useful http://github.com/ochafik/Scalaxy

Page 31: Scala io2013 : Our journey from UML/MDD to Scala macros

Thank you