2. Abstract
• Scala is pretty good for real development.
• However Scala has also some bad points.
• Please note for these points when you introduce
Scala into real development.
3. Scala 300 Recipes from Shoeisha
• Includes 300 Practical Scala
Recipes.
• from Basic Scala topics to
frameworks and tools.
Covers Play2, Akka, sbt, scaladoc, ScalaIDE,
ScalaTest, Specs2, Scalatra, ScalaQuery,
Anorm, Casbah, spray, scala-io, scala-time,
sjson, util-eval, scalaxb, Dispatch and more.
4. Why we are using Scala?
• Decrease costs of system development.
• We felt the limit of Java several years ago.
• Scala might be a breakthrough?
5. About Our Project
• Porting Java based web application to Scala.
• Over 170 HTMLs and 40,000 lines.
• 5 members on 6 months.
Before After
Seasar2 Play2 (Customized)
Apache Click ScalaQuery (Cuztomized)
S2JDBC PostgreSQL
PostgreSQL + Tsearch Apache Solr
Raw JavaScript jQuery + jQuery UI
6. Benefits of Scala
• Decrease amount of source code
▫ 40%-50% OFF
▫ Better abstraction techniques
• Decrease bugs
▫ More type safe
▫ Immutable data types
• Java Interoperability
▫ Many Java based Frameworks and Libraries are
available
▫ Easy to port existing Java software
7. Benefits of Scala
• Decrease amount of source code
▫ 40%-50% OFF
▫ Better abstraction techniques
Flexibility and Safety
• Decrease bugs
▫ More type safe
▫ Immutable data types
• Java Interoperability
▫ Many Java based Frameworks and Libraries are
available
▫ Easy to port existing Java software
8.
9. Long compilation time
• Compilation is so much heavy.
• Painful for large application development.
19. Solution
Original client-side validation framework
based on Play2’s form definition
Original helper to display error messages
20. Anorm is too simple
• We have to write all SQL.
• Magic does not already exist.
• SQL is not type safe.
• No support for dynamic SQL.
21. Solution
Use other ORMs such as ScalaQuery.
We used a combination of scalaquery-magic
scalagen and mirage-scala.
22. Function22 Problem in form definition
• a.k.a. Tuple22 Problem
• Form can not have over 18 properties.
val userForm = Form(
mapping(
"firstName“ -> text,
"lastName“ -> text,
"mailAddress“ -> email,
"password“ -> text,
... Max 18 properties
"tel“ -> text,
"mobile“ -> text,
"company“ -> text,
"department“ -> text
)(UserInfo.apply)(UserInfo.unapply)
)
23. Solution
Nested definition, BUT it’s not expectable.
val userForm = Form(
mapping(
"firstName" -> text,
"lastName" -> text,
"mailAddress" -> email,
"password" -> text,
"companyInfo" -> mapping(
"company" -> text,
"department" -> text
)(CompanyInfo.apply)(CompanyInfo.unapply)
)(UserInfo.apply)(UserInfo.unapply)
)
24. No Servlet and HttpSession
• Hard to port existing Java based web apps.
• We want to run Play2 on the servlet container by
un-technical reasons.
27. solr-scala-client
https://github.com/takezoe/solr-scala-client
• Simple wrapper of SolrJ for Scala.
• Query converter using parser combinator.
import jp.sf.amateras.solr.scala._
val client = new SolrClient("http://localhost:8983/solr")
val result = client.query("name: ?name?")
.getResultAsMap(Map("name" -> "ThinkPad & X201s"))
// => name:("ThinkPad" AND "X201s")
result.documents.foreach { doc: Map[String, Any] =>
...
}
28. scalagen
https://github.com/takezoe/scalagen
• Code Generator from RDBMS.
• Supports Anorm and ScalaQuery in the current version.
• Easy to add support for other ORMs.
• Work as CLI and sbt plugin.
seq(jp.sf.amateras.scalagen.ScalagenPlugin.scalagenSettings: _*)
scalagenConfiguration := jp.sf.amateras.scalagen.Settings(
// for ScalaQuery
generator = new jp.sf.amateras.scalagen.ScalaQueryGenerator(),
// for Anorm
//generator = new jp.sf.amateras.scalagen.ScalaQueryGenerator(),
driver = "org.hsqldb.jdbcDriver",
url = "jdbc:hsqldb:hsql://localhost/",
username = "sa",
password = ""
)
29. scalaquery-magic
https://github.com/shimamoto/scalaquery-magic
• Extends ScalaQuery.
• Make a Magic (like old Anorm’s Magic) by code
generation using scalagen.
val userInfoDao = new UserInfoDao()
// SELECT by ID
val userInfo = userInfoDao.selectById(1)
// INSERT
userInfoDao.insert(UserInfo(DEFAULT[Int], 'userName', 'password'))
// UPDATE
userInfoDao.update(userInfo.copy(userName = 'newName'))
// DELETE by ID
userInfoDao.deleteById(1)
30. mirage-scala
https://github.com/takezoe/mirage-scala
• SQL Centric ORM similar to Anorm.
• Executable SQL template called 2waySQL.
SLEECT USER_ID, USER_NAME, PASSWORD
FROM USER_INFO
/*BEGIN*/
WHERE
/*IF userId != null*/
USER_ID = /*userId*/1
/*END*/
/*IF userId != null*/
USER_NAME = /*userName*/ 'takezoe'
/*END*/
/*END*/
31. play2-httpsession
https://github.com/takezoe/play2-httpsession
• Provide HttpSession for Play2 applications.
• Work with play2-war-plugin.
import jp.sf.amateras.play2.httpsession.HttpSessionSupport._
def index = Action { implicit request =>
// retrieve the object from HttpSession
val value: Option[String] = HttpSession[String]("key")
Ok(value).withHttpSession {
// store objects into HttpSession
"key" -> "value"
}
}