我应该始终使用服务,还是可以直接使用存储库?
当我尝试遵循 DDD 时,我是否应该始终访问这些服务?
或者我可以直接使用存储库来获取域对象吗?
Should I always go through the services when I try to follow DDD?
Or can I use a repository directly to get a domain object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言,我不喜欢在控制器或一般表示层中看到存储库。但我已经看过很多次了,在 DDD 的背景下它没有任何问题。
我认为答案是这取决于你的项目有多大。服务层更常见于更复杂的项目中。而更简单的 MVC 网站则直接使用存储库。
Personally, I don't like seeing repositories in controllers, or in the presentation layer in general. But I've seen it many times and there's nothing wrong with it in the context of DDD.
I think the answer is that it depends on how big your project is. A service layer is more often found in more complex projects. Whereas simpler MVC websites for example just use repositories directly.
你绝对可以。这正是存储库的目标。我想知道您会使用哪种服务来实现此目的(除了在 SOA 或基于 Web 服务的架构的特定上下文中)。
You definitely can. It's precisely the goal of repositories. I wonder what kind of a service you'd otherwise use for that (except in the specific context of an SOA or web service based architecture).
在完成我的第一个使用 DDD 原则构建的项目后:D,我发现拥有可供应用程序层使用的域服务和存储库非常有用。
要点:应用程序层可能是您的 WCF 服务或 Web 服务中的代码(如果您使用该架构)。这一切都取决于您的实施。如果它适合您的实现,您的应用程序层可能与表示层相同,从而使应用程序层代码位于您的控制器或 Web 表单代码后面。
存储库的功能类似于内存中的集合。对于应用程序层来说,代码应该看起来就像您只是在使用任何旧的集合。
领域服务的功能更像是信息的处理器或访问器,这些信息永远不会更新,可能会被处理,但不会直接更新。对于应用程序层来说,代码应该看起来就像您只是在使用任何旧的 Web 服务。
话虽这么说,我将用一些示例进行更多解释:
存储库
我的存储库实际上继承自一个通用集合,该集合使用我创建的实现对象进行类型化,该实现对象将数据库密钥和业务模型封装在一起。使用这种方法,我可以在接口中定义一个索引器,例如
,我可以有一个 getter,它将根据底层集合的索引返回业务对象,还有一个 setter,它将从底层集合中查找数据键并保存对象到数据库。这使得应用程序代码变得非常简单,例如
服务,
我使用服务来检索实体和值对象的列表,我不会单独编辑这些列表,在我的例子中,它们仅用作可用选择或调用聚合根上的方法时的值。通常,我将这些信息缓存在网络服务器上。这不是域服务的唯一用途,只是我的示例。应用层代码仍然比较简单。
总之,根据我对 DDD 原则的理解,应用程序层应该能够从服务或存储库访问业务对象。它们之间的选择取决于最适合领域模型的内容。
After completing my first project structured using the DDD principles :D, I found that it is useful to have both domain services and repositories that are available for the application layer to consume.
KEY POINT: The application layer may be your WCF services or the code in your web services if you're using that architecture. It all depends on your implementation. If it suits your implementation, your application layer may be the same as your presentation layer, thus having the application layer code in your contollers or web forms code behind.
Repositories function like in-memory collections. To the application layer, the code should look like you're just using any old collection.
Domain Services function more like processors or accessors to information that will never be updated, maybe processed, but not updated directly. To the application layer, the code should look like you're just using any old web service.
That being said, I will explain more with some examples:
Repositories
My repositories actually inherit from a generic collection typed with an implementation object I created that encapsulates the database key and the business model together. Using this approach, I can define an indexer in my interface such as
and I can have a getter that will return the business object based on the index of the underlying collection and a setter that will lookup the data key from the underlying collection and save the object to the database. This makes the application code extremely simple, for example
Services
I use services to retrieve lists of entities and value objects that I am not going to edit individually and, in my case, are only used as available selections or values when calling methods on the aggregate root. Generally, I cache this information on the web server. This is not the only use for a domain service, just my example. The application layer code still remains relatively simple.
In conclusion and from my understanding of DDD principles, the application layer should be able to access business objects from either services or repositories. The choice between them comes down to what fits best within the domain model.