Pattern: Multi-Scoped Data/Logic
Intent
The intent for this pattern is to support data/logic usage in multiple scopes. Liferay defines the scopes Global, Site and Page, but from a development perspective scope refers to Portal and individual OSGi Modules. Classic data access implementations do not support multi-scope access because of boundaries between the scopes.
The Multi-Scoped Data/Logic Liferay Design Pattern's intent is to define how data and logic can be designed to be accessable from all scopes in Liferay, either in the Portal layer or any other deployed OSGi Modules.
Also Known As
This pattern is implemented using the Liferay Service Builder tool.
Motivation
Standard ORM tools provide access to data for servlet-based web applications, but they are not a good fit in the portal because of the barriers between modules in the form of class loader and other kinds of boundaries. If a design starts from a standard ORM solution, it will be restricted to a single development scope. Often this may seem acceptable for an initial design, but in the portal world most single-scoped solutions often need to be changed to support multiple scopes. As the standard tools have no support for multiple scopes, developers will need to hand code bridge logic to add multi-scope support, and any hand coding increases development time, bug potential, and time to market.
The motivation for Liferay's Service Builder tool is to provide an ORM-like tool with built-in support for multi-scoped data access and business logic sharing. The tool transforms an XML-based entity definition file into layered code to support multiple scopes and is used throughout business logic creation to add multi-scope exposure for the business logic methods.
Additionally the tool is the foundation for adding portal feature support to custom entities, including:
- Auto-populated entity audit columns.
- Asset framework support (comments, rankings, Asset Publisher support, etc).
- Indexing and Search support.
- Model listeners.
- Workflow support.
- Expando support.
- Dynamic Query support.
- Automagic JSON web service support.
- Automagic SOAP web service support.
You're not going to get this kind of integration from your classic ORM tool...
And with Liferay 7 CE / Liferay DXP, additionally you also get an OSGi-compatible API and service bundle implementation ready for deployment.
Applicability
IMHO Service Builder applies when you are dealing with any kind of multi-scoped data entities and/or business logic; it also applies if you need to add any of the indicated portal features to your implementation.
Participants
The participants in this pattern are:
- An XML file defining the entities.
- Spring configuration files.
- Implementation class methods to add business logic.
- Service consumers.
The participants are used by the Service Builder tool to generate code for the service implementation details.
Details for working with Service Builder are covered in the following sections:
- https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/what-is-service-builder
- https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/running-service-builder-and-understanding-the-generated-code
- https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/defining-an-object-relational-map-with-service-builder
- https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/creating-local-services
- https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/service-builder-web-services
Collaboration
ServiceBuilder uses the entity definition XML file to generate the bulk of the code. Custom business methods are added to the ServiceImpl and LocalServiceImpl classes for the custom entities and ServiceBuilder will include them in the service API.
Consequences
By using Service Builder and generating entities, there is no real downside in the portal environment. Service Builder will generate an ORM layer and provide integration points for all of the core Liferay features.
There are three typical arguments used by architects and developers for not using Service Builder:
- It is not a complete ORM. This is true, it does not support everything a full ORM does. It doesn't support Many To Many relationships and it also doesn't handle automatic parent-children relationships in One To Many. All that means is the code to handle many to many and even some one to many relationship handling will need to be hand-coded.
- It still uses old XML files instead of newer Annotations. This is also true, but this is more a reflection of Liferay generating all of the code including the interfaces. With Liferay adding portal features based upon the XML definitions, using annotations would require Liferay to modify the annotated interface and cause circular change effects.
- I already know how to develop using X, my project deadlines are too short to learn a new tool like Service Builder. Yes there is a learning curve with Service Builder, but this is nothing compared to the mountains of work it will take getting X working correctly in the portal and some Liferay features will just not be options for you without Service Builder's generated code.
All of these arguments are weak in light of what you get by using Service Builder.
Sample Usage
Service Builder is another case of Liferay eating it's own dogfood. The entire portal is based on Service Builder for all of the entities in all of the portlets, the Liferay entities, etc.
Check out any of the Liferay modules from simple cases like Bookmarks through more complicated cases such as Workflow or the Asset Publisher.
Conclusion
Service Builder is a must-use if you are going to do any integrated portal development. You can't build the portal features into your portlets without Service Builder usage.
Seriously. You have no other choice. And I'm not saying this because I'm a fanboy or anything, I'm coming from a place of experience. My first project on Liferay dealt with a number of portlets using a service layer; I knew Hibernate but didn't want to take time out to learn Service Builder. That was a terrible mistake on my part. I never did deal with the multi-scoping well at all, never got the kind of Liferay integration that would have been great to have. Fortunately it was not a big problem to have made such a mistake, but I learned from it and use Service Builder all the time now in the portal.
So I share this experience with you in hopes that you too can avoid the mistakes I made. Use Service Builder for your own good!


