DAO 和服务?

发布于 2024-12-17 23:24:08 字数 304 浏览 5 评论 0原文

我总是面临一个问题,我无法真正想到封装许多 DAO 方法的服务对象。

我的意思是,对于我的 servlet,有时使用单个 DAO 方法就足够了,例如 addUser(User params)。

更好的做法是用服务对象封装 DAO 方法并始终只使用服务对象,即使它字面意思是单个服务方法调用单个 dao 方法或将它们混合使用(一些方法来自服务对象,一些方法来自 servlet 中的 dao) context) - 意味着我在控制器内有自动连接的 DAO 和服务对象?

如果我开始在同一个地方使用 DAO 和 Service 对象,它会混淆逻辑吗?

I'm always facing a problem where I can't really think of service object encapsulating many DAO methods.

I mean that for my servlet sometimes it is sufficient to use single DAO method, for example addUser(User params).

What is better to do - to encapsulate DAO methods with service object and use only service objects ALWAYS, even if it literally means a single service method calling single dao method or mixing their use together(some methods from service objects and some from dao in servlet context) - meaning that I have autowired DAOs and Service objects inside controller ?

It mixes up the logic if I start to use both DAO and Service object in the same place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

街角迷惘 2024-12-24 23:24:08

我认为这个要看具体情况。如果没有 DAO 会导致业务逻辑和数据访问逻辑混合,那么最好有单独的类。

但是,如果您的 DAO 是“虚拟”的并且仅调用 EntityManager 方法,则您可以直接在服务对象中使用它。我们的想法是让类具有单一职责并且易于扩展和测试。您不应该为此而创建图层。

如果您想保留可重用的服务层,我可能不会直接从控制器使用 DAO。如果 DAO 没有意义,我宁愿在服务层中使用 EntityManager(或您正在使用的任何持久性策略)。

I think this depends on the situation. If not having a DAO is going to cause mixing your business logic and your data access logic, it is probably better to have separate classes.

However, if your DAO is "dummy" and just calls an EntityManager method, you can probably use this directly in your service objects. The idea is to have classes that have single responsibilities and are easy to extend and test. You shouldn't be creating layers for the sake of it.

I probably wouldn't use DAOs directly from your controllers if you want to keep a reusable service layer. I would rather use the EntityManager (or whatever persistence strategy you are using) in the service layer if the DAO doesn't make sense.

偏闹i 2024-12-24 23:24:08

我正在使用一个“你不能让控制器与 DAO 交互!”的系统。设计理念在某个时刻被接受,并为每个组件创建了一个服务层。正如您所描述的,大多数服务只是委托给 DAO。我反对这种哲学有两个原因。

其中之一就是那句古老的“你不会需要它”。在需要之前不要实施某些东西。只是因为您预见到某些原因需要额外的间接层来执行一些额外的逻辑,所以不确定您是否需要它。当您最终需要它时,您可能会发现您的期望与您之前的信念并不相符。而且您会付出额外的成本,因为现在您必须对两个类而不是一个类进行单元测试,并且需要在两个位置而不是一个位置添加方法的情况并不罕见。

第二个问题是,服务到底是什么?当我对我的领域进行建模时,我尝试用面向对象的术语进行思考。有用户,因此 User 类才有意义。有新闻项,因此 NewsItem 类有意义。但我什至不知道 UserService 应该做什么。包含用户的“业务逻辑”?不,这就是 User 类的用途。

如果您需要对“外部世界”维护严格的 API,那么我可以看到有一个保持不变的额外层的情况。但在所有其他情况下,您都不需要它。

I am working with a system where the "you cannot have the controllers interact with the DAOs!" design philosophy was embraced at a point and a service layer was created for every component. Most of the services are, as you describe, simply delegating to a DAO. I object to this philosophy for two reasons.

One is the good old "You aren't gonna need it". Don't implement something until you need it. Just because you foresee some reason to have an extra layer of indirection to do some extra logic, it's not sure you're going to need it. And when you end up needing it, you will probably find out that your expectations didn't really match what you believed earlier. And you get an extra cost, because now you have to unit test two classes instead of one, and it's not uncommon that you need to add methods in two places instead of one.

The second is, what the heck is a service anyway? When I model my domain, I try to think in object-oriented terms. There are users, therefore the User class makes sense. There are news items, therefore the NewsItem class makes sense. But I don't even know what a UserService is supposed to do. Contain "business logic" for users? No, that's what the User class is there for.

If you need to maintain a strict API to the "outside world", then I can see a case to have an extra layer that remains unchanged. But in all other cases, you aren't going to need it.

我早已燃尽 2024-12-24 23:24:08

就我个人而言,我通常将 DAO 调用封装在服务中。

这允许我使用 AOP 等使所有服务成为事务性的。并在这些服务中使用非事务性 DAO 方法。

对于琐碎的服务,这是一个额外的“层”,但在我看来,这是一个服务于某种目的的层(并且无论如何都可以使用一种或另一种代码生成来生成它)。不过,我也很少将 DAO 功能封装在服务中。

Personally, I usually encapsulate DAO calls within services.

This allows me to make all services transactional using AOP/etc. and use non-transactional DAO methods within those services.

For trivial services, this is an additional "layer", but IMO one that serves a purpose (and code generation of one sort or another can be used to generate it anyway). It's also rare I have only DAO functionality wrapped up in a service, though.

御守 2024-12-24 23:24:08

视情况而定。将 DAO 和服务层分开的原因通常是:

  • 技术约束(请参阅其他答案中的 AOP 事务)
  • 架构约束(DTO <=> 服务层和 DAO 之间的 @Entities 转换)
  • 历史(这就是它的实现方式) X 年)
  • 美观(仅从视图层访问一层)

使用 Java EE 6(JBoss AS 7),我没有这些负担:

  • 没有 AOP - @Stateless 和 @Transactional 负责交易。
  • 没有 DTO - 我使用从 JPA 到视图层的 @Entities。
  • 别关心历史。
  • 与美观相比,我更喜欢简单/更少的代码。

所以我在DAO层有大部分方法。
对于某些情况,更复杂的操作,我创建一个服务 bean,并且可能使用 扩展持久性上下文

我的经验法则是,一个方法是否应该进入 Service bean:

  1. 如果它使用多个 DAO(显然)
  2. 如果它执行多个实体管理器调用
  3. 如果实现可能会发生变化,例如在 JPQL 中完成的搜索、Hibernate Search 与 ElasticSearch 。

Depends. The reasons to separate DAOs and Service layer are often:

  • the technical constraints (see AOP transactions in other answer)
  • architectural constraints (DTOs <=> @Entities transformation between Service layer and DAO)
  • historical (this is how it was being done for X years)
  • esthetical (to only have one layer accessed from the view layer)

Using Java EE 6 (JBoss AS 7), I don't have these burdens:

  • No AOP - @Stateless and @Transactional takes care of transactions.
  • No DTOs - I use @Entities from JPA up to the view layer.
  • Don't care about history.
  • And I prefer simplicity / less code over esthetics.

So I have most methods in DAO layer.
For some cases, more complex operations, I create a service bean and perhaps use an extended persistence context.

My rule of thumb, whether a method should go into a Service bean:

  1. If it uses multiple DAOs (obviously)
  2. If it performs several entity manager calls
  3. If the implementation is likely to change, e.g. search done in JPQL vs. Hibernate Search vs. ElasticSearch.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文