Message Boards

Decoupling data layer with presentation layer - service builder

Alex Pasquini, modified 4 Years ago.

Decoupling data layer with presentation layer - service builder

Junior Member Posts: 38 Join Date: 3/3/20 Recent Posts
Dear all,I’m quite new on Liferay world. I’m developing with Liferay CE 7.3 ga1 using osgi service builder which generates api and services, creating tables.I’d like to know how can I build a business layer between presentation and data layers. In particular I need the business layer to handle the validation, enrichment, transactional, rollback and orchestration operations between multiple datatables.I thought to use the api layer to do this, anyway the api layer uses the same interfaces of service layer, which I think to be as data layer. I’d like to specify methods that make an aggregate of multiple datatables that I defined as services and give an object which is an aggregate of service interfaces.Even if my question is theoretic, can you please address my needs?
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Decoupling data layer with presentation layer - service builder

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
The service module often ends up performing dual roles of persistence as well as business logic. I like to try and keep the purity of the API module to interfaces and constants, but sometimes it can also be a good place for helper classes.

I absolutely keep any servlet or portlet classes out of the API and service modules. My feeling is that if you are passing a PortletRequest to the service module, you're doing something wrong and corrupting the separation of layers.

Validation and verification, those concepts are not always so clean to separate out. Obviously from a persistence perspective, you only want to save valid data, so you should always have appropriate checks in prior to actual persistence. But this form of validation checking may not be appropriate to provide necessary visual feedback to the user such as marking field(s) as missing or failing validation, so you might also want to have validation and verification logic here to support providing that kind of feedback.

Transactional, rollback and orchestration are to be done in the service module. Liferay will wrap transactions around the service builder methods so it is an obvious place for this. So you can have a method like addAllOfMyStuff(...) and it in turn can invoke other service methods. The transaction will be propagated so all of these things can be done within a single transaction scope. Throw an exception in any part and the rollback occurs.
Alex Pasquini, modified 4 Years ago.

RE: Decoupling data layer with presentation layer - service builder

Junior Member Posts: 38 Join Date: 3/3/20 Recent Posts
Hi David,
thank you for your answer.
I agree in almost everything even if I'd like to handle the validation in both Presentation and Business layer. The first to avoid dirty data flows to the business layer, the second to ensure that data which can come from other channels would give me problems.
Anyway I'd like to decrease the abstraction level and give you an example.
Basically I have two services:
1. Document: includes commercial data and also the path of these documents;
2. PriceList: this service takes the DocumentType as well as the user role and retrieves the price of the document for a given user/role.
Since services are part of different Business Units, I'd like to develop them through two different service builder.
The requirement is that the presentation layer calls need the document data and the valid price from price list.
I designed the flow in this way:
1. Presentation layer calls Document service using userId, serviceContext and type of document I need to retrieve
2. Document receive the call, retrieve documents and for each document calls the PriceList service
3. PriceList service handle the price calculation and gives back the result.

In this scenario I implemented the second and third points on the related LocalServiceImpl.
Anyway in this case I need to specify the field "price" on Document service.xml since I need to send back to the presentation layer the document data and the price. This means I'm forced to have a price column even in the Document table in order to have this property on Document interface shared with presentation layer. Isn't it?
Thank you in advance.
Best regards,

Alex
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Decoupling data layer with presentation layer - service builder

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
Actually you have a couple of options here, but I would probably go with the following...

In the document in the <entity /> tag, do not include price as it is not going to be persisted. But you can add a price to the DocumentImpl model class. This will make it a transient value (not persisted) but is capable of holding the value for passing it around.

Your DocumentLocalServiceImpl can use the PriceLocalService to get the associated Price instance, then set the price in the Document before returning.
Alex Pasquini, modified 4 Years ago.

RE: Decoupling data layer with presentation layer - service builder

Junior Member Posts: 38 Join Date: 3/3/20 Recent Posts
Thank you David