使用 Spring 进行领域驱动编程

发布于 2024-10-18 21:30:14 字数 324 浏览 3 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-10-25 21:30:14

我认为应用程序的“外观”应该使用存储库(或其他基础设施服务)来保存“书”。本书不应该自行保存,这是存储库的责任。

如果您需要从域实体进行任何基础设施操作(例如,搜索数据库),那么您应该通过查找上下文(并因此耦合到 Spring)或通过依赖项注入存储库来访问此存储库注入实体。

问题是实体的“实例化”不是 Spring 的责任,而是 Persistence Provider 的责任,因此 Spring 无法处理这种注入。该怎么办?

好吧,有几种方法(它们都不是非常“漂亮”)可以做到这一点:

  • 通过 AOP:您可以使用面向方面的框架(如 AspectJ)来检测代码,配置系统以在实例化时刻注入实体中的任何依赖项。
  • 通过 Hibernate 拦截器:如果您的持久性提供程序是 Hibernate,它会为您提供一个挂钩,将拦截器放置在实体生命周期的某些点。您可以配置一个拦截器来查找 spring 上下文,以在每个实体的实例化中注入依赖项。
  • 也许最简单的方法是实现一个小的静态“serviceLocator”,与 spring 相结合,在实体需要时查找实体请求的服务。这个服务定位器只是一个避免实体耦合到 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:

  • Through AOP: you can instrument the code with Aspect Oriented Framework (like AspectJ) configuring the system to inject whatever dependency in the Entity in the instantiation moment.
  • Through a Hibernate interceptor: if your Persistence Provider is Hibernate, it offers you a hook to place interceptors in certain points of the life cycle of the Entities. You can configure an interceptor that looks-up the spring context to inject dependencies in the instantiation of every Entity.
  • Maybe the easiest way is to implement a little and static "serviceLocator" coupled with spring that looks-up services asked by the Entities when they need them. This service locator is just a layer to avoid your entities to get coupled to Spring.
心房的律动 2024-10-25 21:30:14

我认为“保存”方法(例如保存在数据库中)不属于域对象......这本书是否“保存”本身?或者存储库正在保存它?...

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?...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文