存储库模式中与集合接口无关的查询
Queries in repository pattern which are not related to the collection interface
Repository 表示集合接口。您可以使用存储库存储、删除和查找对象。
但我经常看到存储库的接口包含封装了与集合接口无关的复杂查询的方法。
例如:复杂计算统计的方法,其中 returns 一些 dto。或者使用 mysql 其中 return 布尔值的一些检查,例如 "userHasSomething"
似乎存储库不是这些方法的最佳位置。
Repository 应该严格表示集合的接口还是应该完成与存储相关的所有工作?
在哪里放置这些查询?
坦率地说,所有这些存储库内容都是基于意见的。
以下是 Martin Fowler 对其的定义:
A system with a complex domain model often benefits from a layer, such as the one provided by Data Mapper (165), that isolates domain objects from details of the database access code. In such systems it can be worthwhile to build another layer of abstraction over the mapping layer where query construction code is concentrated. This becomes more important when there are a large number of domain classes or heavy querying. In these cases particularly, adding this layer helps minimize duplicate query logic.
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. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.
如您在问题中所述:
But i often see interface of repository contains methods encapsulated complex queries which are not related to collection interface.
在我的意见中,在 DDD 上下文中,存储库应该按照您在问题中解释的方式(集合接口)工作。其余部分是业务逻辑,应移至域模型或服务。
理论仍然是理论;纯粹主义者严格遵守它。最重要的是业务需求。
规律是好的,必须遵循。这些是由专家根据他们多年的经验构建的。如果遇到同样的问题,应该毫不犹豫地使用它。
存储库等模式比 GoF 模式更广泛。这使得存储库位基于意见。此外,存储库也广泛用于 DDD 上下文之外。这进一步增加了意见。
It seems a Repository is not the best place for these methods.
如果您这么认为,并且在您的设计中有更好的地方可以使用这些方法,请继续将这些方法移到那个地方。如果您的设计告诉您存储库是这些方法的最佳位置,请毫不犹豫地使用它。
Where to place these queries?
由你决定。专注于手头的问题,专注于你的设计,专注于你的业务需求。不要在您的代码中添加不必要的复杂性 只是为了 正确地遵循模式。如果通过修复承诺的模式而产生新问题,该模式有什么用?
Repository 表示集合接口。您可以使用存储库存储、删除和查找对象。
但我经常看到存储库的接口包含封装了与集合接口无关的复杂查询的方法。 例如:复杂计算统计的方法,其中 returns 一些 dto。或者使用 mysql 其中 return 布尔值的一些检查,例如 "userHasSomething"
似乎存储库不是这些方法的最佳位置。
Repository 应该严格表示集合的接口还是应该完成与存储相关的所有工作?
在哪里放置这些查询?
坦率地说,所有这些存储库内容都是基于意见的。
以下是 Martin Fowler 对其的定义:
A system with a complex domain model often benefits from a layer, such as the one provided by Data Mapper (165), that isolates domain objects from details of the database access code. In such systems it can be worthwhile to build another layer of abstraction over the mapping layer where query construction code is concentrated. This becomes more important when there are a large number of domain classes or heavy querying. In these cases particularly, adding this layer helps minimize duplicate query logic.
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. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.
如您在问题中所述:
But i often see interface of repository contains methods encapsulated complex queries which are not related to collection interface.
在我的意见中,在 DDD 上下文中,存储库应该按照您在问题中解释的方式(集合接口)工作。其余部分是业务逻辑,应移至域模型或服务。
理论仍然是理论;纯粹主义者严格遵守它。最重要的是业务需求。
规律是好的,必须遵循。这些是由专家根据他们多年的经验构建的。如果遇到同样的问题,应该毫不犹豫地使用它。
存储库等模式比 GoF 模式更广泛。这使得存储库位基于意见。此外,存储库也广泛用于 DDD 上下文之外。这进一步增加了意见。
It seems a Repository is not the best place for these methods.
如果您这么认为,并且在您的设计中有更好的地方可以使用这些方法,请继续将这些方法移到那个地方。如果您的设计告诉您存储库是这些方法的最佳位置,请毫不犹豫地使用它。
Where to place these queries?
由你决定。专注于手头的问题,专注于你的设计,专注于你的业务需求。不要在您的代码中添加不必要的复杂性 只是为了 正确地遵循模式。如果通过修复承诺的模式而产生新问题,该模式有什么用?