我们应该 save/update 存储库模式中的模型吗?
Should we save/update models in repository pattern?
我正在学习存储库模式,并且看到了很多使用存储库模式进行创建和更新的示例。
这是 repisitory 接口的一个示例。
interface RepositoryInterface
{
public function all();
public function create(array $data);
public function update(array $data, $id);
public function delete($id);
public function show($id);
}
此存储库接口负责 creating/retrieving 和更新模型。
但是,经过更好的搜索后,我发现人们应该避免将数据保存在存储库中,存储库应该充当集合并且仅用于检索数据。这是 link .
这是他们在那里所说的。
Probably the most important distinction about repositories is that they represent collections of entities. They do not represent database storage or caching or any number of technical concerns. Repositories represent collections. How you hold those collections is simply an implementation detail.
这是一个仅检索数据的存储库示例。
interface BlogRepositoryInterface
{
public function all();
public function getByUser(User $user);
}
我想知道存储库模式的最佳实践是什么?
如果我们应该只使用存储库来检索模型,那么我们如何处理 create/update/delete 个模型?
我认为你误解了你引用的句子:
Probably the most important distinction about repositories is that
they represent collections of entities. They do not represent database
storage or caching or any number of technical concerns. Repositories
represent collections. How you hold those collections is simply an
implementation detail.
没有声明说您应该只使用存储库进行阅读。存储库最重要的特征是,当您使用存储库创建或更新项目时,更改可能不会立即应用到持久层。应用时间更改取决于存储库的实现。
我的一个小提示,我们不应该在存储库中有一个名为 create
的方法。作为一个集合,我们向其中添加项,而不是创建项。在我的存储库接口中,我通常有一个 add
方法而不是 create
方法。创造应该是一个工厂的责任。
存储库模式完全允许对象持久化。
摘自 Martin Fowler 的书企业应用程序架构模式(第 322 页):
A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes.
摘录很清楚:由于 Repository 是一个集合,您应该可以随意添加和删除其中的对象。
我唯一担心的是你的界面。你应该把它分成两个或更多,因为你可能会有这样的对象:
- 不打算删除
- 不打算更新
- 不打算插入
创建不同的接口将使您的代码符合接口隔离原则,该原则规定不应强迫客户端依赖它不使用的方法。
一些示例:
假设您有一个 class 代表您所在国家/地区的一个州。很少看到一个国家经常添加新州、删除或更改州名。因此,class State
可以实现一个只有方法 all()
和 show()
.
的接口
假设您正在编写电子商务代码。从数据库中删除 Customer
不是一种选择,因为他的所有数据(如购买历史、搜索等)都将丢失。所以你会做一个软删除,设置一个标志 $customer->deleted = true;
。在这种情况下,class Customer
可以实现一个接口,该接口只有方法 all()
和 show()
以及其他接口 - 或两个接口 - 用于方法 insert()
和 update()
.
我正在学习存储库模式,并且看到了很多使用存储库模式进行创建和更新的示例。 这是 repisitory 接口的一个示例。
interface RepositoryInterface
{
public function all();
public function create(array $data);
public function update(array $data, $id);
public function delete($id);
public function show($id);
}
此存储库接口负责 creating/retrieving 和更新模型。
但是,经过更好的搜索后,我发现人们应该避免将数据保存在存储库中,存储库应该充当集合并且仅用于检索数据。这是 link .
这是他们在那里所说的。
Probably the most important distinction about repositories is that they represent collections of entities. They do not represent database storage or caching or any number of technical concerns. Repositories represent collections. How you hold those collections is simply an implementation detail.
这是一个仅检索数据的存储库示例。
interface BlogRepositoryInterface
{
public function all();
public function getByUser(User $user);
}
我想知道存储库模式的最佳实践是什么?
如果我们应该只使用存储库来检索模型,那么我们如何处理 create/update/delete 个模型?
我认为你误解了你引用的句子:
Probably the most important distinction about repositories is that they represent collections of entities. They do not represent database storage or caching or any number of technical concerns. Repositories represent collections. How you hold those collections is simply an implementation detail.
没有声明说您应该只使用存储库进行阅读。存储库最重要的特征是,当您使用存储库创建或更新项目时,更改可能不会立即应用到持久层。应用时间更改取决于存储库的实现。
我的一个小提示,我们不应该在存储库中有一个名为 create
的方法。作为一个集合,我们向其中添加项,而不是创建项。在我的存储库接口中,我通常有一个 add
方法而不是 create
方法。创造应该是一个工厂的责任。
存储库模式完全允许对象持久化。
摘自 Martin Fowler 的书企业应用程序架构模式(第 322 页):
A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes.
摘录很清楚:由于 Repository 是一个集合,您应该可以随意添加和删除其中的对象。
我唯一担心的是你的界面。你应该把它分成两个或更多,因为你可能会有这样的对象:
- 不打算删除
- 不打算更新
- 不打算插入
创建不同的接口将使您的代码符合接口隔离原则,该原则规定不应强迫客户端依赖它不使用的方法。
一些示例:
假设您有一个 class 代表您所在国家/地区的一个州。很少看到一个国家经常添加新州、删除或更改州名。因此,class
State
可以实现一个只有方法all()
和show()
. 的接口
假设您正在编写电子商务代码。从数据库中删除
Customer
不是一种选择,因为他的所有数据(如购买历史、搜索等)都将丢失。所以你会做一个软删除,设置一个标志$customer->deleted = true;
。在这种情况下,classCustomer
可以实现一个接口,该接口只有方法all()
和show()
以及其他接口 - 或两个接口 - 用于方法insert()
和update()
.