我对laravel中的存储库模式有一个疑问,我会在我的项目中使用存储库模式,但我的项目像电子商务网站(比如flipkart、ebay、amazon..(,目前我有80多个表。将来我会添加很多表,那么如何有效地处理提供者中的绑定方法呢?(我的示例代码如下(
class RepoServiceProvider extends ServiceProvider
{
public function register(){
$this->app::bind(
'AppRepositoriesUserUserRepositoryInterface',
'AppRepositoriesUserUserRepository');
// Binding another repository if has multiple repository
// $this->app->bind(
// 'AppRepositoriesPostRepositoryInterface',
// 'AppRepositoriesPostRepository'
// );
//Another approach of binding repository
// $this->app->bind(
// CustomerRepositoryInterface::class,
// CustomerRepository::class
// );
}
}
您可以尝试注入具体的存储库,而不是抽象/接口,容器将解析它们。
或者,您可以使用";当/然后";您的服务提供商中的链:
$this->app->when(UserService::class)->needs(UserRepositoryInterface::class)->give(UserRepository::class);
对于服务提供商中需要RepositoryInterface的每个服务(或任何类(,您都必须重复此操作。这样做的目的是在特定类需要抽象时为其绑定具体化,这就是为什么您需要为每个需要接口的类执行相同的绑定。