在Spring环境中进行域驱动设计和交易 [英] Domain driven design and transactions in Spring environment

查看:126
本文介绍了在Spring环境中进行域驱动设计和交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经设计出我的应用程序在贫血域模型,所以我有很多存储库对象,被注入到大,胖,事务感知的服务层。此模式称为事务脚本。它不被认为是一个很好的做法,因为它导致了程序代码,所以我想转移到域驱动的设计。

I used to design my application around anemic domain model, so I had many repository object, which were injected to the big, fat, transaction-aware service layer. This pattern is called Transaction script. It's not considered a good practice since it leads to the procedural code, so I wanted to move forward to the domain driven design.

在阅读了几篇文章在网络上,听着克里斯·理查森(Chris Richardson)在Parleys上的讲话,并阅读了行动中的POJO的DDD章节,我想我得到了很大的一张。

After reading couple of articles on the web, listening to the Chris Richardson's talk on Parleys and reading DDD chapters of the POJOs in Action, I think I got the big picture.

问题是,我不知道,如何在我的应用程序中组织事务。 Chis Richardson在他的书中指出:

Problem is, that I don't know, how to organize transactions in my application. Chis Richardson in his book states:


演示层通过直接调用
的域模型来处理来自用户浏览器的HTTP请求或间接地通过外墙,如上一章中描述的I
是POJO或EJB。

The presentation tier handles HTTP requests from the user’s browser by calling the domain model either directly or indirectly via a façade, which as I described in the previous chapter is either a POJO or an EJB.

好到目前为止,但是关于 InfoQ文章的Srini Penchikala指出:

Good so far, but Srini Penchikala on InfoQ article states:


一些开发人员喜欢管理DAO类中的交易,这是一个糟糕的设计。这导致了细微的事务控制,这并不能够灵活地管理事务跨越多个域对象的用例。服务类应处理事务;这样即使事务跨多个域对象,服务类可以管理事务,因为在大多数用例中,Service类处理控制流。

Some developers prefer managing the transactions in the DAO classes which is a poor design. This results in too fine-grained transaction control which doesn't give the flexibility of managing the use cases where the transactions span multiple domain objects. Service classes should handle transactions; this way even if the transaction spans multiple domain objects, the service class can manage the transaction since in most of the use cases the Service class handles the control flow.

好的,所以如果我明白这一点,存储库类不应该是事务性的,服务层(现在更薄)是事务性的(如以前在事务脚本模式中)。但是如果域对象被表示层直接调用呢?这是否意味着我的域对象应该有事务性行为?如何在Spring或EJB环境中实现它?

Ok, so if I understand this correctly, repository classes should not be transactional, service layer (which is now much thinner) is transactional (as it used to be in Transaction script pattern). But what if domain objects are called by presentation layer directly? Does it mean, that my domain object should have transactional behavior? And how to implement it in Spring or EJB environment?

这对我来说似乎有点奇怪,所以如果有人澄清,我会很高兴。谢谢。

This seems kind of weird to me, so I'd be happy if somebody would clarify that. Thank you.

推荐答案

到目前为止,我个人对DDD和Spring应用DDD是一种无状态的事务服务层并通过它访问域对象。所以我这样做的方式,领域模型根本不了解交易,完全由服务处理。

My personal take on applying DDD with Spring and Hibernate, so far, is to have a stateless transactional service layer and access the domain objects through that. So the way I'm doing it the domain model does not know about transactions at all, that is handled entirely by the services.

您可能会找到一个示例应用程序来看看。看起来埃里克·埃文斯参与创作。

There is an example application you might find helpful to take a look at. It looks like Eric Evans was involved in creating it.

这篇关于在Spring环境中进行域驱动设计和交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆