특정 응용 프로그램에서 작업하면서 필자는 매우 비슷한 쿼리를 계속해서 계속 작성합니다. 정확히 동일하지는 않지만 매우 유사한 형태이며 거의 동일한 코드 덩어리 인 에 삽입됩니다.,DRY 및 이와 유사한 쿼리
$Mysqli = new mysqli;
if ($Stmt = $Mysqli->prepare("SELECT foo
FROM tblFoo
WHERE something = ?")) {
$Stmt->bind_param('s', $this->_something);
$Stmt->execute();
if (0 != $Stmt->errno)
throw new Exception("blah, blah, blah");
$Stmt->bind_result($foo);
while ($Stmt->fetch()){
$this->_foos[] = new Foo($foo);
}
$Stmt->close();
} else {
throw new Exception("blah, blah, blah"););
}
}
이상 다른 곳에서 ...
$Mysqli = new mysqli;
if ($Stmt = $Mysqli->prepare("SELECT bar, baz
FROM tblBar
WHERE somethingElse = ?")) {
$Stmt->bind_param('s', $this->_somethingElse);
$Stmt->execute();
if (0 != $Stmt->errno)
throw new Exception("blah, blah, blah");
$Stmt->bind_result($bar, $baz);
while ($Stmt->fetch()){
// do something else with $bar and $baz
}
$Stmt->close();
} else {
throw new Exception("blah, blah, blah"););
}
}
... 그리고 또 다른, 다른 곳에서 또 다른 ... 등
이 DRY의 실제 위반 ? 이러한 종류의 쿼리 (테이블, 열, 바운드 변수 등의 생성자 params 또는 setter 포함)를 수행하고 내 앱 전체에서 다시 사용하는 클래스를 작성하는 것은 당연한 것처럼 보입니다. 그러나 동시에, 나는 자신을 반복하고있는 잔소리 같은 느낌을 떨칠 수 없습니다.
단순한 쿼리를 작성하는 방법이 너무 많아서 이와 같은 반복이 예상되는 것일 수도 있습니다.
생각하십니까?
@ 키비 - 고마워요. 나는 Hibernate를 사용하지 않을 것이지만, 나는 ActiveRecord 클래스를 생성 한 다음 특정 테이블에 대해 몇 가지 클래스를 subbed했다. – PartialOrder