Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I started using the Play Framework a couple of months ago. It "redefines" many conventions from the traditional Java world (like the way of using static methods among other things). I was browsing the documentation when I found this piece of code in scala:

package controllers

import play.api.Play.current
import play.api.mvc._
import play.api.db._

object Application extends Controller {

  def index = Action {
    var outString = "Number is "
    val conn = DB.getConnection()
    try {
      val stmt = conn.createStatement
      val rs = stmt.executeQuery("SELECT 9 as testkey ")
      while (rs.next()) {
        outString += rs.getString("testkey")
      }
    } finally {
      conn.close()
    }
    Ok(outString)
  }

}

Basically, it is a controller with a database call on it. I understand that Play Framework is revolutionary in some aspects, but, isn't this against the very basic Layer Service Pattern? What's the rationale behind being fine to use a SQL call on a Controller?

EDIT:

The reason I got to this Play tutorial page is because I was looking for the most common practice in Play about this particular case, I've found more than one case in a real life application where this is happening. So my Question, to be more precise: Is this an accepted practice in Play? is this just "tolerated" in some circumstances (for a real life code, not in a tutorial)? or the SQL query simply must go in a "model" object?

share|improve this question
2  
this is a tutorial example (SSCCE) indended to show absolutely minimal code strictly necessary for demonstrated functionality to work. Readers are expected to add whatever complexities they need in their, more complicated applications developed after they understand the example in tutorial. See What should take precedence: YAGNI or Good Design? - "The most simple designs are the most easy to evolve..." – gnat Mar 19 '16 at 11:23
    
@gnat if you found code like this in a production environtment, would you be happy with this as this apparently follows the YAGNI principle? – morgano Mar 19 '16 at 11:46
    
I would be enormously happy if production code (and requirements) were as simple as in that tutorial. "SELECT 9 as testkey " – gnat Mar 19 '16 at 12:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.