如何使用 Laravel 自动注入依赖
How to Auto Inject Dependency with Laravel
我正在尝试构建一个在 lumen 上使用依赖注入的结构。
我有服务层和存储库层。
我想将存储库层注入到服务层。让我尝试向您展示代码
interface IUserRepostitory {
public function getByID($id);
}
class UserRepository extends BaseRepository implements IRepository{
public function getByID($id) {
//Please don't think how this function works, my question about dependency injection
return $this->findOrFail($id);
}
}
interface IService {
public function getByID($id);
}
class UserService implements IService{
private $Repository;
public __construct(IUserRepositor $UserRepository) {
$this->Repository = $UserRepository
}
public function getByID($id) {
return $this->Repository->getByID($id);
}
}
我在这里注册依赖解析器。
//Dependency resolver for Repository Layer
class RepositoryServiceProvider extends ServiceProvider {
public function register()
{
$this->app->singleton(IUserRepository::class, function () {
return new UserRepository();
});
}
}
我在这里注册服务层
class ServiceServiceProvider extends ServiceProvider {
public function register()
{
$this->app->singleton(IUserService::class, function () {
//Here is what I don't like
//It would be great a solution that automaticly resolve UserRepository.
return new UserService(new UserRepository());
});
}
}
如您所见,我想自动解析对 UserService 的依赖。但是单例方法需要创建返回对象。
有更好的方法吗?
*** 注意:请不要注意语法,我是在 lumen 上写的,但在 laravel.
上也有同样的问题
一旦将 UserRepository
绑定到 IUserRepository
,您就可以通过 make
函数解析来实例化 IUserService
和 IUserRepository
!
修改你的 ServiceServiceProvider
这样:
class ServiceServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(IUserService::class, function ($app) {
return new UserService($app->make(IUserRepository::class));
});
}
}
我正在尝试构建一个在 lumen 上使用依赖注入的结构。
我有服务层和存储库层。
我想将存储库层注入到服务层。让我尝试向您展示代码
interface IUserRepostitory {
public function getByID($id);
}
class UserRepository extends BaseRepository implements IRepository{
public function getByID($id) {
//Please don't think how this function works, my question about dependency injection
return $this->findOrFail($id);
}
}
interface IService {
public function getByID($id);
}
class UserService implements IService{
private $Repository;
public __construct(IUserRepositor $UserRepository) {
$this->Repository = $UserRepository
}
public function getByID($id) {
return $this->Repository->getByID($id);
}
}
我在这里注册依赖解析器。
//Dependency resolver for Repository Layer
class RepositoryServiceProvider extends ServiceProvider {
public function register()
{
$this->app->singleton(IUserRepository::class, function () {
return new UserRepository();
});
}
}
我在这里注册服务层
class ServiceServiceProvider extends ServiceProvider {
public function register()
{
$this->app->singleton(IUserService::class, function () {
//Here is what I don't like
//It would be great a solution that automaticly resolve UserRepository.
return new UserService(new UserRepository());
});
}
}
如您所见,我想自动解析对 UserService 的依赖。但是单例方法需要创建返回对象。
有更好的方法吗?
*** 注意:请不要注意语法,我是在 lumen 上写的,但在 laravel.
上也有同样的问题一旦将 UserRepository
绑定到 IUserRepository
,您就可以通过 make
函数解析来实例化 IUserService
和 IUserRepository
!
修改你的 ServiceServiceProvider
这样:
class ServiceServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(IUserService::class, function ($app) {
return new UserService($app->make(IUserRepository::class));
});
}
}