存储库模式是否过度杀伤力
我使用存储库模式(DDD 和 POEAA)已经有一段时间了。然而,我们的一些团队成员认为这只是一个额外的抽象层,没有必要。 我可以从他们的论点中看到一些好处。现代 ORM 解决方案(NHibernate 或 EF)几乎拥有您所需的一切。我搜索并找到了一些类似 this 的文章 和 反驳。那么存储库模式是否有点过大了?
I have been using Repository pattern (DDD and POEAA) for some time. However some of our team members have argued that it is just an extra layer of abstraction and unnecessary.
I can seen some benefit in their arguments. Modern ORM solutions (NHibernate or EF) have almost everything you need. I searched and found some article like this and counterargument on this topic. So Is repository pattern an overkill?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这主要取决于问题的复杂性以及领域模型在解决方案中扮演的角色。对于简单的解决方案,存储库可能有点大材小用。但对于具有强大语言和不断变化的需求的复杂领域,存储库是一个很好的、干净的抽象,它拥有领域对象的生命周期。许多 ORM 都会做很多这样的事情,但是,在复杂的领域中,总会有一些领域活动在存储库中有意义,但开箱即用的 ORM 不支持这些活动。
底线:这取决于上下文。
It depends, mostly on the complexity of your problem and the role your Domain Model plays in the solution. For simple solutions, Repository is probably overkill. But for complex domains with a robust language and evolving needs/requirements, the Repository is a nice, clean abstraction that owns the domain object lifecycle. Many ORMs will do much of this, but, in a complex domain, there will always be some domain activity that makes sense in a repository and which is not supported by an ORM out of the box.
Bottom line: it depends on the context.
在单元测试中模拟数据访问是我使用存储库接口的主要原因。另一个原因是可维护性 - 您可以轻松实现缓存策略,或切换到其他数据访问实现,例如从服务而不是数据库获取数据。
Mocking data access in unit tests is the main reason I use repository interfaces. Another reason maintainability - you can easily implement caching strategies, or switch to other data access implementation, like getting data from the service instead of DB.
我们在项目中使用存储库的原因之一是它强制我们的聚合根是谁(我们只允许 AR 的存储库),以便您正确地使用 AR,而不是查询您喜欢的任何内容。
正如 Al 提到的……它确实提供了一个很好的接口,可以在单元测试期间进行模拟。
The one reason we use Repositories in our project is that it enforces who our Aggregate Roots are (we only allow repositories for ARs) so that you work through the AR properly instead of querying for whatever strikes your fancy.
And as Al mentioned.. it does provide a nice interface to mock out during unit tests.