如果我在我的Laravel 5项目中使用andersao/l5-repository,我会破坏控制反转原则吗?
Will I break the inversion of control principle if I use andersao/l5-repository in my Laravel 5 project?
我是 Laravel 开发新手,我正在努力像一名优秀的程序员一样理解和应用 SOLID
原则。所以我最近在 laravel.
中学习并应用了存储库模式
为此,我创建了一个目录存档并使用 psr-4
加载它,如下所示:
"Archive\": "archive/"
然后我创建了一个名为 Repositories
的文件夹和另一个名为 Contracts
的文件夹。现在在 Contracts 文件夹中我有 interfaces
像 UserRepositoryInterface
和 ServicesRepositoryInterface
等等,在 Repositories
文件夹外面,我有像 DbUserRepository
和 DbServiceRepository
等等。
我正在使用一个名为 DataServiceProvider
的 service provider
,我像这样绑定它们:
$this->app->bind(UserRepositoryInterface::class, DbUserRepository::class);
$this->app->bind(ServiceRepositoryInterface::class, DbServiceRepository::class);
所以我可以通过这种方式在我的控制器中注入 Contacts
,例如 UserRepositoryInterface
和 ServiceRepositoryInterface
,并且 Laravel 会自动从 IoC 容器中解析我的依赖项。因此,如果将来我需要一个 FileUserRepository
,我只需要创建那个 class 并更改我的 service provider
中的绑定,我的控制器就不会出现任何问题。
这是我从泰勒和杰弗里那里学到的。但是现在我正在尝试为我的项目使用一个包 https://github.com/andersao/l5-repository。
据此,我将 extend
我的 DbUserRepository
与它附带的 BaseRepository 像这样:
namespace App;
use Prettus\Repository\Eloquent\BaseRepository;
class UserRepository extends BaseRepository {
/**
* Specify Model class name
*
* @return string
*/
function model()
{
return "App\Post";
}
}
现在,我显然重用了 BaseRepository
附带的所有代码和功能,例如 all()
、panginate($limit = null, $columns = ['*'])
、find($id)
等等,但现在我我打破了控制反转原则,因为现在我必须将具体的实现注入到我的控制器中?
我仍然是一名新手开发人员,正在尝试理解所有这些,并且在解释事情时可能在问题的某个地方出错了。在控制器中保持松散耦合的同时使用包的最佳方法是什么?
没有理由你仍然不能实现你的接口:
namespace App;
use Prettus\Repository\Eloquent\BaseRepository;
class DbUserRepository extends BaseRepository implements UserRepositoryInterface {
/**
* Specify Model class name
*
* @return string
*/
function model()
{
return "App\Post";
}
}
但是,您现在遇到了一个问题;如果你换掉你的实现,你的 UserRepositoryInterface
中没有任何内容表明 BaseRepository
方法也必须被实现。如果你看一下 BaseRepository
class,你应该看到它实现了它自己的两个接口:RepositoryInterface
和 RepositoryCriteriaInterface
和 'luckily' php 允许 multiple interface inheritence
,这意味着您可以按如下方式扩展 UserRepositoryInterface
:
interface UserRepositoryInterface extends RepositoryInterface, RepositoryCriteriaInterface {
// Declare UserRepositoryInterface methods
}
然后您可以正常绑定和使用您的界面:
$this->app->bind(UserRepositoryInterface::class, DbUserRepository::class);
我是 Laravel 开发新手,我正在努力像一名优秀的程序员一样理解和应用 SOLID
原则。所以我最近在 laravel.
为此,我创建了一个目录存档并使用 psr-4
加载它,如下所示:
"Archive\": "archive/"
然后我创建了一个名为 Repositories
的文件夹和另一个名为 Contracts
的文件夹。现在在 Contracts 文件夹中我有 interfaces
像 UserRepositoryInterface
和 ServicesRepositoryInterface
等等,在 Repositories
文件夹外面,我有像 DbUserRepository
和 DbServiceRepository
等等。
我正在使用一个名为 DataServiceProvider
的 service provider
,我像这样绑定它们:
$this->app->bind(UserRepositoryInterface::class, DbUserRepository::class);
$this->app->bind(ServiceRepositoryInterface::class, DbServiceRepository::class);
所以我可以通过这种方式在我的控制器中注入 Contacts
,例如 UserRepositoryInterface
和 ServiceRepositoryInterface
,并且 Laravel 会自动从 IoC 容器中解析我的依赖项。因此,如果将来我需要一个 FileUserRepository
,我只需要创建那个 class 并更改我的 service provider
中的绑定,我的控制器就不会出现任何问题。
这是我从泰勒和杰弗里那里学到的。但是现在我正在尝试为我的项目使用一个包 https://github.com/andersao/l5-repository。
据此,我将 extend
我的 DbUserRepository
与它附带的 BaseRepository 像这样:
namespace App;
use Prettus\Repository\Eloquent\BaseRepository;
class UserRepository extends BaseRepository {
/**
* Specify Model class name
*
* @return string
*/
function model()
{
return "App\Post";
}
}
现在,我显然重用了 BaseRepository
附带的所有代码和功能,例如 all()
、panginate($limit = null, $columns = ['*'])
、find($id)
等等,但现在我我打破了控制反转原则,因为现在我必须将具体的实现注入到我的控制器中?
我仍然是一名新手开发人员,正在尝试理解所有这些,并且在解释事情时可能在问题的某个地方出错了。在控制器中保持松散耦合的同时使用包的最佳方法是什么?
没有理由你仍然不能实现你的接口:
namespace App;
use Prettus\Repository\Eloquent\BaseRepository;
class DbUserRepository extends BaseRepository implements UserRepositoryInterface {
/**
* Specify Model class name
*
* @return string
*/
function model()
{
return "App\Post";
}
}
但是,您现在遇到了一个问题;如果你换掉你的实现,你的 UserRepositoryInterface
中没有任何内容表明 BaseRepository
方法也必须被实现。如果你看一下 BaseRepository
class,你应该看到它实现了它自己的两个接口:RepositoryInterface
和 RepositoryCriteriaInterface
和 'luckily' php 允许 multiple interface inheritence
,这意味着您可以按如下方式扩展 UserRepositoryInterface
:
interface UserRepositoryInterface extends RepositoryInterface, RepositoryCriteriaInterface {
// Declare UserRepositoryInterface methods
}
然后您可以正常绑定和使用您的界面:
$this->app->bind(UserRepositoryInterface::class, DbUserRepository::class);