0
我一直在大量閱讀與laravel一起使用存儲庫模式,並且我已經看到了使用存儲庫本身以及其接口完成的DI。存儲庫是否需要一個接口?
我的問題是,存儲庫是否需要一個有效的接口?
class Repository {
public function foo() {
return "Hi";
}
}
class RepositoryController {
private $test;
public function __contruct(Repository $test) {
$this->test = $test;
}
public function do() {
echo $test->foo();
}
}
VS:
interface RepositoryInterface {
function foo (string $text)
}
class Repository implements RepositoryInterface {
public function foo ($text) {
return $text;
}
}
class RepositoryController {
private $test;
public function __contruct(RepositoryInterface $test) {
$this->test = $test;
}
public function do() {
echo $test->foo();
}
}
「有效」---這究竟意味着什麼? – zerkms
@zerkms我認爲存儲庫模式的重點是通過抽象操作數據而不觸及模型。使用界面可以實現這一點。但我想知道這個的原因。 – Shawn31313