Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid repeating business logic between client and server?

As the needs of web apps have grown, I have found myself writing more and more API driven web applications. I use frameworks like AngularJS to build rich web clients that communicate with these APIs. Currently I am using PHP (Lumen or Laravel) for the server side / API.

The problem is, I find myself repeating business logic between the client and the server side often.

When I say business logic I mean rules like the following for an order form:

  • You can buy X if you buy Y.
  • You cannot buy Y if you have Z.
  • If you buy 10 of these you get 10% off.
  • Height x Width x Depth x Cost = Final Cost.
  • Height must be between 10 and 20 if your width is greater than 5.
  • Etc etc.

To make this app both responsive and fast, the logic for calculations (along with other business logic) is being done on the client side. Since we shouldn't trust the client, I then re-verify those numbers on the server side. This logic can get pretty complex and writing this complex logic in both places feels dangerous.

I have three solutions in mind:

  1. Make everything that require business logic make an ajax call to the API. All the business logic would live in one place and can be tested once. This could be slow since the client would have to wait for each and every change they make to the order form to get updated values and results. Having a very fast API would help with this. The main downside is that this may not work well when users are on poor connections (mobile devices).

  2. Write the business logic on the client side AND on the server side. The client gets instant feedback as they make changes on the form, and we validate all data once they submit on the server. The downside here is that we have to duplicate all the business logic, and test both sides. This is certainly more work and would make future work fragile.

  3. Trust the client!?! Write all the business logic on the client side and assume they didn't tamper with the data. In my current scenario I am working on a quote builder which would always get reviewed by human so maybe this is actually ok.

Honestly, I am not happy about any of the solutions which is why I am reaching out to the community for advice. I would love to hear your opinions or approaches to this problem!

like image 441
Roeland Avatar asked May 28 '16 18:05

Roeland


People also ask

Is business logic performed on the client side?

Business logic contains business rules. Application logic (and presentation logic) can be implemented on client-side. Business logic only on server-side.

What is server-side business logic?

The server-side business logic consists of the following: An IS document type. This document type, referred to as the business document type, defines the structure of the data you are synchronizing. Adapter services. These services connect to the backend application and retrieve the data to synchronize.

Which server will execute business logic on?

Business logic almost always has to run on a server you control, for security reasons. If by "server" you mean "web server", then I agree, it doesn't need to have almost any business logic.


2 Answers

You can do one more thing.

Create your validation and business logic code with JavaScript only. But make it very loosely coupled, as much as possible. If possible, only take JSON as input and give JSON as output.

Then set up a separate NodeJS server alongside the existing PHP server to serve that logic to the client, so that on the client side it can be used without an AJAX call.

Then from the PHP server, when you need to validate and run all those business logic rules, use cURL to call the NodeJS business logic and validate the data. That means an HTTP call from the PHP server to the NodeJS server. The NodeJS server will have additional code which will take the data, validate with the same code, and return the result.

By this way you can make

  1. Faster development - one place to unit test your logic.
  2. Faster client code execution - no need for AJAX, since the same validation JavaScript code is being served by NodeJS to your client.
  3. All business logic lives in the NodeJS server - when business logic changes, you only need to touch this part; so that in the near future, if you need to create some other additional interfaces, then you can use this server to validate your data. It will work just like your Business Rule Server.

The only thing you need to do is setup a NodeJS server alongside your PHP server. But you do not need to change all of your code to run on the NodeJS server.

like image 127
Partha Sarathi Ghosh Avatar answered Sep 24 '22 21:09

Partha Sarathi Ghosh


I had the same issue when I decided to create an application using Laravel for back end, and Angular 2 for front-end. And it seems to me there is no solution to avoid the business logic duplicate so far, because:

At the moment PHP and JavaScript cannot be converted from one to another. Would it be nice if we can use same language for writing the business logic and then embed them into both back-end and front-end. From this point it leads me to another point:

To achieve the goal, we should write the business logic in one language only, and so far JavaScript is the best solution. As you know TypeScript/EMCA Script help us to write the code in the OOP way. Meteor framework NodeJS infrastructure help us to write code in JavaScript for running in both sides Back-end and front-end.

So from my point of view, we can use TypeScript/EMCA to write packages for business logic, for example a class for validation written in JavaScript can be implemented both side, so you just write one time only, but it will be called twice from front-end and back-end also.

That's my point. Hope to see some other solutions for this very interesting topic.

like image 23
Tinh Dang Avatar answered Sep 21 '22 21:09

Tinh Dang