简体   繁体   English

客户端的业务逻辑

[英]Business Logics on the client side

Why do people say that business logics should be implemented on the server side code (eg EJB) and not on the client application code? 为什么人们说业务逻辑应该在服务器端代码(例如EJB)上实现而不是在客户端应用程序代码上实现?

The example that I have in mind is a business object validation on a EJB based architecture. 我想到的示例是基于EJB的体系结构上的业务对象验证。 Does it really have to be delegated to the EJB or is it ok to run it on the client before the object is sent to the server to be saved? 是否真的必须委托给EJB,还是可以在将对象发送到要保存的服务器之前在客户端上运行它?

Validation can be done client-side in order to save the round-trip to the server. 验证可以在客户端完成,以便保存到服务器的往返。 But the server should also perform the same validation. 但是服务器也应该执行相同的验证。 Ideally, you can reuse the same classes on both client and server-side. 理想情况下,您可以在客户端和服务器端重用相同的类。

There are two reasons. 有两个原因。 First, you're likely to have the same business logic for multiple clients, so it makes sense to factor it out to the server. 首先,您可能为多个客户端提供相同的业务逻辑,因此将其分解到服务器是有意义的。 Second, you most likely can't trust the client, which means server validation is essential. 其次,您很可能无法信任客户端,这意味着服务器验证至关重要。

Why do people say that business logics should be implemented on the server side code (eg EJB) and not on the client application code? 为什么人们说业务逻辑应该在服务器端代码(例如EJB)上实现而不是在客户端应用程序代码上实现?

That's something we've learned from client/server (fat clients) architectures. 这是我们从客户端/服务器(胖客户端)架构中学到的东西。 With such architectures, you had to redeploy all the clients to change something and this was just a pain. 有了这样的架构,你必须重新部署所有客户端来改变一些东西,这只是一个痛苦。 Server side applications and centralized deployment (with thin or rich clients) don't have this problem and are thus preferred. 服务器端应用程序和集中部署(使用瘦客户端或富客户端)没有此问题,因此是首选。

The example that I have in mind is a business object validation on a EJB based architecture. 我想到的示例是基于EJB的体系结构上的业务对象验证。 Does it really have to be delegated to the EJB or is it ok to run it on the client before the object is sent to be server to be saved? 是否真的必须委托给EJB,或者在将对象发送到要保存的服务器之前在客户端上运行它是否可以?

Validation is something that you must perform on the server side ("don't trust the client") and can also perform on the client side to avoid a round trip. 验证是,你必须在服务器端执行(“不信任客户端”),也可以在客户端执行,以避免往返东西。 And I'm not even mentioning the case of multiple clients (a GUI, a command line application, a batch, etc). 我甚至没有提到多个客户端(GUI,命令行应用程序,批处理等)的情况。

Because usually the client code is used to display the information and not really to define what the application does. 因为通常客户端代码用于显示信息,而不是真正定义应用程序的功能。

Mainly because this gives you flexibility to add other clients without having to do strange tricks ( like copy/paste ) 主要是因为这使您可以灵活地添加其他客户端,而无需做任何奇怪的技巧(如复制/粘贴)

For instance, assume your client is using HTML, later you want to add a new client using json. 例如,假设您的客户端使用HTML,以后您想使用json添加新客户端。

If the business rules were on the client, you'll have to copy them to the new client. 如果业务规则在客户端上,则必须将它们复制到新客户端。

Now, don't get confused with validation rules and other. 现在,不要混淆验证规则和其他。 Those have to be placed in the client side too. 那些也必须放在客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM