Creating a data access layer

Let's bring together everything that we have seen and build a data-mapper class for fetching Physicist objects from the database. These classes (also called data access objects) are useful to decouple the internal representation of an object from its representation in the database.

We start by defining the Physicist class:

// Physicist.scala
case class Physicist(
  val name:String,
  val gender:Gender.Value
)

The data access object will expose a single method, readAll, that returns a Vector[Physicist] of all the physicists in our database:

// PhysicistDao.scala import java.sql.{ ResultSet, Connection } import Implicits._ // implicit conversions object PhysicistDao { /* Helper method for reading a single row */ private def ...

Get Scala: Guide for Data Science Professionals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.