在多层架构中,我可以跳过业务层进行crud操作吗? [英] In multi-layered architecture, can I skip the business layer for crud operations?

查看:165
本文介绍了在多层架构中,我可以跳过业务层进行crud操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有3层应用程序,每个来自服务层的调用都通过数据层进入业务层和peresist。
每个图层的组件只能调用下面的图层;

We have 3 layered application wich every call from service layer goes to business layer and peresist by data layer. Components of each layer can only call the layer below;

然而,因为我们有数百个实体,而且我们有很多与crud operatins相关的服务,所以我们的团队提出了很多不和之处。

However because we have hundreds of entities and we have lots of services related to crud operatins so many contoverseries raised on our team.

有些人认为,为了维护和易于开发,最好从crud服务调用数据访问,这只是进行crud操作并绕过业务层。

Some believe for the sake of maintenance and ease of development it's better to call data access from crud services which just doing crud operation and bypassing business layer.

相反,有些人说我们必须为业务层中每个实体的数据访问创建包装器,并从服务中调用这些包装器,并且永远不允许服务调用数据访问层。

On the contrary some saying we have to create wrapper for data access of each entity in business layer and call these wrapper from services and never allow services to call data access layer.

在你的想法中我们应该采取哪种方式? crud服务是否可以调用数据访问并绕过业务层?

In your idea which way we should take? Is it ok for crud services to call data accesses and bypassing business layer?

推荐答案

如果没有业务逻辑可执行,那么没有理由强制执行业务层。 3层架构不是一个神秘的协议,只是假设业务处理形成的最佳实践。

If there is no business logic to perform, there is no reason to enforce a business layer. The 3-tier architecture is not an arcane protocol, just a best practice that was formed assuming business processing.

在当前的应用程序中,当没有涉及业务流程时,我们经常直接从JSF控制器访问DAO。这个想法是由 java冠军提出的,他强调简单性至关重要。

In a current application we are often accessing DAOs directly from JSF controllers when there is no business process involved. The idea was given by a java champion who stressed the idea that simplicity is paramount.

如果您担心未来可能需要添加业务逻辑的修改。我以这种方式思考问题:无论如何,额外的业务逻辑将被添加到业务层,包括数据访问,因此这里没有区别。

If you are worried about future modifications that may require adding business logic. I think of the issue this way: The additional business logic would be added to the business layer anyway, including data access, so there is no difference here.

CRUD代码大多非常简单。因此,服务的更改将相当于将DAO中的单个调用或几个调用重新路由到EJB - 一个简单的重构。 CRUD代码本身仍然存在,但将被推入EJB - 另一种简单的重构。

CRUD code is mostly very simple. So the change in the service would amount to reroute a single call or a couple of calls fron the DAO to an EJB - a simple refactoring. The CRUD code itself would still remain, but will be pushed into the EJB - another simple refactoring.

这并不完美,但IMO比其他方案更好:拥有一个空的间接层。这增加了无用的复杂性。业务对象只会将调用转发给DAO。

This is not perfect, but IMO better then the alternative: having an empty indirection layer. This adds complexity that serves no purpose. The business object would do nothing but forward the calls to the DAO.

有两个代码味道,我认为适用于这种情况:人为的复杂性和功能羡慕

There are two code smells that I think apply in this case: contrived complexity and feature envy.

我不是说业务层中的DA在某种程度上是代码味道。我的意思是拥有一个没有别的的业务对象但代理DAO是一种气味。它与复杂性相同 - 增加了数据结构/架构层,没有任何用途 - 您的应用程序中似乎已经存在DAL。

I am not saying that DA in the business layer is somehow a code smell. What I mean is that having a business object that does nothing else but proxy the DAO is a smell. It's the same with complexity - an added data structure / architectural layer that serves no own purpose - there seems to be a DAL in your application already.

您考虑的另一个方面是 - 开发人员看到直接使用DAO的服务有多么令人惊讶?有5个服务,其中2个直接访问DAO不同于拥有100个服务,其中只有一个服务直接访问DAO。

Another aspect for your consideration would be - how surprising is it for a developer to see a service that uses a DAO directly? Having 5 services where 2 of them access DAO directly is different from having 100 services where only one service accesses the DAOs directly.

在第一种情况下,代码简单性将超过增加概念复杂性(单个事物的2个概念),在第二种情况下,我宁愿坚持业务层 - 意外(也称为WTF效应;)以不同的方式做一次只会太大。

In the first case the code simplicity would outweigh the added conceptual complexity (2 concepts for a single thing), in the second case, I would rather stick with the business layer - the surprise (also called the WTF-effect ;) of doing it differently just once would be too big.

这篇关于在多层架构中,我可以跳过业务层进行crud操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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