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