简体   繁体   English

表示逻辑还是业务逻辑?

[英]Presentation logic or business logic?

Lets imagine I have three buttons in my html page. 假设我的html页面中有三个按钮。 The first button is for selecting a car, the second is for renting that car and the next button is for paying the rent. 第一个按钮用于选择汽车,第二个按钮用于租赁汽车,第二个按钮用于支付租金。 All the three buttons make an ajax request to do some business logic in the server. 所有这三个按钮都发出ajax请求,以在服务器中执行一些业务逻辑。 When the user press the first button and the ajax request is ok, I enable the second button and disable de first button and the same for the second and third. 当用户按下第一个按钮并且ajax请求正常时,我启用第二个按钮,禁用第一个按钮,第二个和第三个按钮相同。 By default all buttons are disabled. 默认情况下,所有按钮都是禁用的。 This enabling or disabling is in javascript if the ajax requests are ok. 如果ajax请求正常,则在javascript中启用或禁用此功能。 I think the enabling or disabling buttons or permissions, is bussiness logic and should be present not in the view but in the server. 我认为启用或禁用按钮或权限是业务逻辑,不应在视图中而是在服务器中显示。 I mean, would not be better to make an ajax request to get the permissions the user has according to the operations he has already done? 我的意思是,根据自己已经执行的操作发出ajax请求以获得用户拥有的权限会更好吗?

Thanks a lot 非常感谢

If you want your server side code to decide on whether to display the next button you could do something like this: 如果您希望服务器端代码决定是否显示下一个按钮,则可以执行以下操作:

$.get("/api/yourController/yourMethod")
    .success(function (response) {
        if (response == true){ //change this for your unique decision logic
            // execute show button code
        }
    });

Based on the data you return from your server in the response object, you can control whether your next button appears in your HTML. 根据您从服务器在response对象中返回的数据,您可以控制下一个按钮是否出现在HTML中。

There are two answers I have: 我有两个答案:

  • It is entirely reasonable to have an ajax request to get the available state transitions. 有一个ajax请求来获取可用的状态转换是完全合理的。 You talk about this as if it is permissions, but that's not really true. 您说的好像是权限,但这不是真的。 Permissions are state that differs between users based on what that user is permitted to do. 权限是根据用户被允许执行的操作而在用户之间不同的状态。 This is what operations are valid given a particular state based on business rules, which is a fine thing to have a request to get. 这是根据业务规则在给定特定状态的情况下有效的操作,这是获得请求的好方法。

  • That said, by the time you're using AJAX, it's common to have an MVC pattern on the client as well as server. 就是说,当您使用AJAX时,在客户端和服务器上都具有MVC模式是很常见的。 That is, you have some view and controller logic in your HTML page in addition to the view logic. 也就是说,除了视图逻辑之外,HTML页面中还具有一些视图和控制器逻辑。 This logic implements some business rules. 该逻辑实现了一些业务规则。 Angular and other client-side frameworks make patterns like this explicit. Angular和其他客户端框架使这种模式变得很明确。 In such an application the server view layer is thin. 在这样的应用程序中,服务器视图层很薄。 It is still important to make sure that important business rules are enforced in your server so that an attacker cannot subvert those rules on the client. 确保在服务器中实施重要的业务规则仍然很重要,这样攻击者才能在客户端上破坏这些规则。 In such an application, it would be entirely reasonable to have buttons enabled on the client side so long as the server made sure you did not rent or pay for a car without selecting it. 在这样的应用程序中,只要服务器确保您没有选择就不租车或买车,在客户端启用按钮是完全合理的。

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

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