DDD 中的存储库层
我读了这篇文章,对此我有一个疑问。
存储库模式是一个外观,它抽象了你的持久性 远离您的域。一方面它假装是一个收藏品——另一方面 另一个涉及你坚持不懈的技术问题 实施。
这是否意味着您可以像创建集合一样命名存储库中的方法。例如:
addDomainModel(...)
getDomainModel(...)
等等?或者您会说出这些方法:
saveDomainModel(...)
fetchDomainModel(...)
什么是最好的或最正确的?我应该让方法名称说明它添加的内容,还是只是:
add(...)
get(...)
就像在普通集合中一样?
http://devlicio.us/blogs /casey/archive/2009/02/20/ddd-the-repository-pattern.aspx
I read this article and I have a question about it.
The Repository pattern is a Facade, that abstracts your persistence
away from your Domain. On one side it pretends to be a collection – on
the other it deals with the technical concerns of your persistence
implementation.
Does that mean that you name the methods in repository as you would if you made a collection. For example:
addDomainModel(...)
getDomainModel(...)
and so on? Or would you name the methods:
saveDomainModel(...)
fetchDomainModel(...)
What would be best, or most correct? And should I have the method names saying what it adds, or just:
add(...)
get(...)
as it would in a normal collection?
http://devlicio.us/blogs/casey/archive/2009/02/20/ddd-the-repository-pattern.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对此没有任何要求。您可以以最自然的使用方式命名这些方法。
通常我的存储库看起来像这样(c# 代码)
如您所见,我使用不同的存储库来更新持久性和查询持久性。虽然第一个方法具有非常常见的 CRUD 方法来处理将要创建/更新/删除的实体,但另一个方法只是列出或搜索数据库并返回适合视图的模型。您可以根据使用情况添加或命名方法,您不必将存储库实现为一个集合(即实现预定义的接口),只需将其视为一个集合即可。重要的是要保持域与持久性实现无关。
There is no requirement for that. You name the methods in the most natural way for usage.
Usually my repository looks like this (c# code)
As you see I'm using different repositories for updating persistence and for querying persistence. While the first one has pretty much common CRUD methods for deaing with entities which will be created/updated/deleted, the other one just lists or searches the db and returns a model suitable for views. You can add or name methods depending on the usage, you don't HAVE to implement the repository as a collection (i.e implementing a predefined interface), just treat it like one. The important thing is to keep the domain agnostic from the persistence implementation.
DDD 的目标之一是领域专家可以阅读代码。因此,您应该命名您的方法以使代码更具可读性。从这个角度来看,在我看来,itemRepository.add(item) 甚至 items.add(item) 比 itemRepository.saveItem(item) 更容易理解。看看这个:
如何编写存储库
One of the goals of DDD, is that domain experts can READ the code. So you should name your methods to make your code more readable. From this point of view, in my opinion, itemRepository.add(item) or even items.add(item) is more understandable than e.g. itemRepository.saveItem(item). Look at this:
How to Write a Repository