REST is not appropriate for business applications because of necessary to distribute business logic accross layers. REST alternative required!

angularjsbusiness-logicrestspring

I have Spring+Java Server Faces (Facelets) application for which I would like to create Single Page Application (SPA) version, e.g. using AngularJS (which is the best and most popular GUI framework). But after some investigation of technologies I have come to conclusion that REST (which is necessarity for AngularJS) ir bad choice for complex business applications, because there is requirement to divide business logic accross software layers.

Let me explain this. In Spring+JSF application all the business logic was encoded in special layer – Spring entities and services. In Spring+REST+AngluarJS this should be different. I don't want to rewrite (move) business logic from Spring/Java to AngularJS/JavasScript but obviously I am required to to. There are one issue and 3 types of business logic in my application that requires consideration.

  • Issue one. Spring provided stateful interaction with business object. E.g. one could open new invoice, add customer, add invoice lines, recalculate discounts, sales tax, totals, add shipping information and so on. Session and business objects were stored in Spring server side session. REST architecture requires that session abd business objects are stores in client (Browser JavaScript objects). So, migration required almost complete rewrite of applicateion.
  • Business logic type 1. Validatione. It was possible to include validation rules in Spring server side objects. REST requires that validation is done in JavaScript objects, because it is impossible upon each change of each field to send complete client state to the server for validation.
  • Business logic type 2. Change monitoring and propagation. E.g. if user adds invoice line with article to the invoice inside application form, then program can retrieve all the discounts that are relevant for this customer, this article and this moment of time. Discounts can be applied to the invoice line and changes can be propagated to update invoice sales tax and totals. It is almost impossible to do this type of change propagation in the REST, if part of the business object in session are stored in the server side.
  • Business logic type 3. Processing of business objects. E.g. if invoice is marked as ordered then Spring application can make extensive changes in other database objects – e.g. update inventory levels, plan shipping activities, create accounting entriess and so on. Spring could do this and Spring will still be able to do this in REST application. This is the only type of business logic that I can retaing from my initial application.

So – it seems to me that migration from Spring+JSF to Spring+AngluarJS requires rewrite of almost three fourths (3/4) of application. Does adding new type of GUI (and new type of communication – REST) to existing application should be so involved?

I am thinking about two problemts

  • Alternative to REST. All my migration problems come from the requirement that AngularJS should use REST for communication with server state (Spring) objects and session. Maybe there are alternatives to REST? JSF/facelets is already doing lot of JavaScript for for communication with server, maybe AngluarJS can be adapted to this?
  • I am uneasy about writing business logic in JavaScript. We are doing lot of work for automatic generation of Java code from models and we would like to keep this experience. There are lot of strange thins about OO JavaScript. And beside Java is researched a lot more in this regard.

So – the main question is – is there alternative to REST for SPA business application (AngularJS) communication with the stateful business logic that is implemented in the server side application (Spring/Java/Enterprise Java)?

Best Answer

There is a gross misconception somewhere. I think it is near this idea.

In Spring+JSF application all the business logic was encoded in special layer - Spring entities and services. In Spring+REST+AngluarJS this should be different. I don't want to rewrite (move) business logic from Spring/Java to AngularJS/JavasScript but obviously I am required to

No, that isn't obvious at all. Why would the implementation details of your application state require you to rewrite your domain model? Those are completely orthogonal concerns.

REST architecture requires that session and business objects are stored in client (Browser JavaScript objects).

Not at all. REST architecture calls for sending representations of application state to the client.

Put very simply, the client and the server are just sending messages back and forth, so that the client can successfully navigate the application protocol. The side effects that the application protocol has on the domain model are limited to the server.

Review Jim Webber's How to GET a cup of coffee.