使用 Spring 进行领域驱动编程
我有一个关于 DDD 和 DDD 的问题春天。我总是围绕贫乏的领域模型和服务设计我的应用程序,照顾业务逻辑/持久性。
假设您有一个用于域对象(例如 Book)的 spring 管理的持久性/存储库服务。如果我必须在书中公开 save() 方法,那么我将需要在我的域内使用存储库 bean,否则我将必须查找存储库 bean 的上下文。这与依赖注入正好相反。
现在,如果我将存储库 ID 注入到域中,并且域对象被缓存(集群缓存),然后在反序列化时,它将不会有注入的存储库服务,因为 Spring 容器会有所不同。
我可能是错的,但如果有人可以向我解释这个场景是如何工作的,那将会有很大的帮助
I have a question on DDD & Spring. I always design my application around anemic domain model and service taking care of business logic/persistence.
Assume you have a spring managed persistence/respository service for a Domain object e.g. Book. If I have to expose a save() method on book then i will need repository bean inside my domain or i will have to look up the context for the repository bean. Which is exactly opposite of Dependency Injection.
Now, If I have repository id injected into domain and domain object is cached ( clustered cache) and then on deserialization it will not have the injected repository service as spring containers will be different.
I might be wrong but if someone can explain me how this scenario will work, it would be of great help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为应用程序的“外观”应该使用存储库(或其他基础设施服务)来保存“书”。本书不应该自行保存,这是存储库的责任。
如果您需要从域实体进行任何基础设施操作(例如,搜索数据库),那么您应该通过查找上下文(并因此耦合到 Spring)或通过依赖项注入存储库来访问此存储库注入实体。
问题是实体的“实例化”不是 Spring 的责任,而是 Persistence Provider 的责任,因此 Spring 无法处理这种注入。该怎么办?
好吧,有几种方法(它们都不是非常“漂亮”)可以做到这一点:
I think that the "facade" of your application should use a Repository (or other infrastructure service) to save the "book". The book should not save it-self, this is responsibility of the Repository.
If you need to make any infrastructure operation (for example, search the database) from a Domain Entity, then you should gain access to this Repository by looking up the context (and getting coupled to Spring as a result) or injecting the Repository through Dependency Injection in the Entity.
The problem is that the "instantiation" of the Entity is not responsibility of Spring but of the Persistence Provider, so Spring cannot handle this injection. What to do?
Well, there are several ways (none of them very "beautyful") to do this:
我认为“保存”方法(例如保存在数据库中)不属于域对象......这本书是否“保存”本身?或者存储库正在保存它?...
I think that a "save" method (save in a DB, in example) doesn't belong to the domain object... Does the book "save" itself? Or is the repository saving it?...