当不同实体的工作大不相同时,我应该如何管理通用存储库模式?
How should I manage Generic Repository Pattern when the works of different entities are pretty much different?
我是 Repository 模式的新手。
当我管理多个实体(例如:客户、订单等)的 CRUD
操作时,这很好。我正在制作一个界面,我正在制作一个通用存储库。这符合我的目的,因为 CRUD 操作对他们来说很常见。
我的问题是:
当几个实体的职责完全不同,它们之间没有共同的方法,这种情况下怎么办?我是否应该为这些特定目的增加接口和存储库的数量?或者在最佳实践方面有没有更好的解决方案?
How should I manage Generic Repository Pattern when the works of different entities are pretty much different?
这是Generic Repository模式的核心问题;这就是为什么它被认为是 anti-pattern.
我读了这个here:
No matter what clever mechanism I tried, I always ended up at the same problem: a repository is a part of the domain being modeled, and that domain is not generic. Not every entity can be deleted, not every entity can be added, not every entity has a repository. Queries vary wildly; the repository API becomes as unique as the entity itself.
为什么通用存储库是反模式?
- 存储库是正在建模的域的一部分,并且该域不是通用的。
- 并非每个实体都可以删除。
- 并非每个实体都可以添加
- 并非每个实体都有存储库。
- 查询千差万别;存储库 API 变得与实体本身一样独特。
- 对于
GetById()
,标识符类型可能不同。
- 无法更新特定字段 (DML)。
- 通用查询机制是 ORM 的职责。
- 大多数 ORM 公开了与通用存储库非常相似的实现。
- 存储库应该使用 ORM 公开的通用查询机制来实现对实体的特定查询。
- 无法使用复合键。
- 它无论如何都会在服务中泄露 DAL 逻辑。
- 谓词标准如果你接受作为参数需要从服务层提供。如果这是 ORM 特定的 class,它会将 ORM 泄漏到服务中。
我建议您阅读这些 (1, 2, 3, 4, 5) 篇解释为什么通用存储库是反模式的文章。
更好的方法是:
- 跳过通用存储库。实施具体的存储库。
- 使用通用存储库作为抽象基础存储库。从中派生所有具体存储库。
在任何情况下,都不要将通用存储库暴露给调用代码。另外,不要从具体存储库公开 IQueryable
。
我是 Repository 模式的新手。
当我管理多个实体(例如:客户、订单等)的 CRUD
操作时,这很好。我正在制作一个界面,我正在制作一个通用存储库。这符合我的目的,因为 CRUD 操作对他们来说很常见。
我的问题是:
当几个实体的职责完全不同,它们之间没有共同的方法,这种情况下怎么办?我是否应该为这些特定目的增加接口和存储库的数量?或者在最佳实践方面有没有更好的解决方案?
How should I manage Generic Repository Pattern when the works of different entities are pretty much different?
这是Generic Repository模式的核心问题;这就是为什么它被认为是 anti-pattern.
我读了这个here:
No matter what clever mechanism I tried, I always ended up at the same problem: a repository is a part of the domain being modeled, and that domain is not generic. Not every entity can be deleted, not every entity can be added, not every entity has a repository. Queries vary wildly; the repository API becomes as unique as the entity itself.
为什么通用存储库是反模式?
- 存储库是正在建模的域的一部分,并且该域不是通用的。
- 并非每个实体都可以删除。
- 并非每个实体都可以添加
- 并非每个实体都有存储库。
- 查询千差万别;存储库 API 变得与实体本身一样独特。
- 对于
GetById()
,标识符类型可能不同。 - 无法更新特定字段 (DML)。
- 通用查询机制是 ORM 的职责。
- 大多数 ORM 公开了与通用存储库非常相似的实现。
- 存储库应该使用 ORM 公开的通用查询机制来实现对实体的特定查询。
- 无法使用复合键。
- 它无论如何都会在服务中泄露 DAL 逻辑。
- 谓词标准如果你接受作为参数需要从服务层提供。如果这是 ORM 特定的 class,它会将 ORM 泄漏到服务中。
我建议您阅读这些 (1, 2, 3, 4, 5) 篇解释为什么通用存储库是反模式的文章。
更好的方法是:
- 跳过通用存储库。实施具体的存储库。
- 使用通用存储库作为抽象基础存储库。从中派生所有具体存储库。
在任何情况下,都不要将通用存储库暴露给调用代码。另外,不要从具体存储库公开 IQueryable
。