Play Framework 2.5.x Scala Slick implementation style

空扰寡人 提交于 2019-12-12 18:13:19

问题


I have kind of philosophical question.

I have been a very happy user of Play Framework for Java for couple years now. Now I am trying to dive into Scala and functional programming. In Java-based play I have been using Ebean, so according to Play documentation I extended Ebean Model class and implemented my own models. In each model I declared a static variable of type Finder in order to call queries. All of this is documented and working well.

However in Scala-based Play (v2.5.x) there is not too much documentation about persistance layer. OK, I understood there is a recommendation of Play Slick as it is using the ideas of functional programming. I am kind of excited about that, but there is almost no documentation on how to use it. I found a way on how to enable Slick, how to configure data source and db server and how to inject db into Controller. There is also a very small example on how to call simple query on db.

The question is: How to actually use Slick? I researched some third party tutorials and blogs and it seems there are multiple ways.

1) How to define models? It seems that I should use case classes to define model itself. Than I should define class extending Table to define columns and its properties??

2) What is the project structure? Should I create new scala file for each model? By conventions of Java I should, but sometimes I have seen all models in one scala file (like in Python Django). I assume separate files are better.

3) Should I create DAOs for manipulating Models? Or should I create something like Service? The code would be probably very same. What I am asking is the structure of the project.

Thank you in advance for any ideas


回答1:


I had the same questions about slick and came up with a solution that works for me. Have a look at this example project:

https://github.com/nemoo/play-slick3-example

Most other example projects are too basic. So I created this project with a broader scope, similar to what I found in real live play code. I tested out various approaches, including services. In the end I found the additional layer hard to work with because I never knew where to put the code. You can see the thought process in the past commits :)

Let me quote from the readme: Repositories handle interactions with domain aggregates. All public methods are exposed as Futures. Internally, in some cases we need to compose various queries into one block that is carried out within a single transaction. In this case, the individual queries return DBIO query objects. A single public method runs those queries and exposes a Future to the client.




回答2:


I can wholeheartedly recommend the Getting Started part of the Slick documentation

There is also a Typesafe Activator Template for Slick - Hello Slick - which you can find here and then explore and continue from there

  1. To get started with Slick and Play you would need to add the dependency in your build.sbt file:

    "com.typesafe.play" %% "play-slick" % "2.0.0"
    

    Also evolutions (which I recommend)

    "com.typesafe.play" %% "play-slick-evolutions" % "2.0.0"
    

    And of course the driver for the database

    "com.h2database" % "h2" % "${H2_VERSION}" // replace `${H2_VERSION}` with an actual version number
    
  2. Then you would have to specify the configuration for your database:

    slick.dbs.default.driver="slick.driver.H2Driver$"
    slick.dbs.default.db.driver="org.h2.Driver"
    slick.dbs.default.db.url="jdbc:h2:mem:play"
    

If you want to have a nice overview of all this and more you should definitely take a look at THE BEST STARTING POINT - a complete project with Models, DAOs, Controllers, adapted to Play 2.5.x



来源:https://stackoverflow.com/questions/36616810/play-framework-2-5-x-scala-slick-implementation-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!