title: Building a generic DAO for slick
body: <p>I was tired of always doing something like the following in order to do database access using slick for <strong>each</strong> of my domain entities.</p>
  
  <pre><code>database withSession {
    implicit session =&gt;
      val entities = TableQuery[EntityTable]
      val id = //Some ID
      val q = for {
        e &lt;- entities if e.id === id
      } yield (e)
      val entity = q.first
  }
  </code></pre>
  
  <p><em>(Note: EntityTable was defined like described <a href="http://slick.typesafe.com/doc/2.0.0/schemas.html#tables">here</a>)</em></p>
  
  <p>So I decided that I want a generic database access object that handles this for me. The usage should look something like</p>
  
  <pre><code>[...]
  val entityDAO = new GenericDAO[Entity, EntityTable, String]()
  [...]
  
  database withSession {   implicit session =&gt;
      val id = // Some ID
      val entity = entityDAO.get(id)
  }
  </code></pre>
  
  <p>My try of the implementation of the GenericDAO looks like this</p>
  
  <pre><code>class GenericDAO[T, TB, PK](implicit session: Session) {
    val entities = TableQuery[TB] // Line 1
  
    def get(id: PK): T = {
      val q = for {
        e &lt;- entities
      } yield (e)
      val res: T = q.first
      res
    }
  }
  </code></pre>
  
  <p>But line 1 leaves me with a compiler error stating that something is wrong with the TB argument.</p>
  
  <blockquote>
    <p>Multiple markers at this line
      - type arguments [TB] conform to the bounds of none of the overloaded alternatives of value apply: [E &lt;:
    scala.slick.lifted.AbstractTable[<em>]]=>
    scala.slick.lifted.TableQuery[E,E#TableElementType]     [E &lt;:
    scala.slick.lifted.AbstractTable[</em>]](cons: scala.slick.lifted.Tag =>
    E)scala.slick.lifted.TableQuery[E,E#TableElementType]
      - wrong number of type parameters for overloaded method value apply with alternatives: [E &lt;: scala.slick.lifted.AbstractTable[<em>]]=>
    scala.slick.lifted.TableQuery[E,E#TableElementType]  [E &lt;:<br>
    scala.slick.lifted.AbstractTable[</em>]](cons: scala.slick.lifted.Tag =>
    E)scala.slick.lifted.TableQuery[E,E#TableElementType]</p>
  </blockquote>
  
  <p>Any suggesions on this issue? Or maybe I am wrong and it is supposed to be implemented in another way. I am open for any solution. Thanks!</p>