Implementation Principles for Services
Various aspects of the ServiceLayer architecture should be taken into account when implementing services. The more of these aspects you use, the more transparent and modular your service implementation becomes. However, you can implement any service to be as complex in architecture as your project requires and allows.
A minimum service implementation consists of:
-
A service implementation for the business logic. Services must be implemented by you explicitly.
-
Models for data management. Models are generated automatically by the SAP Commerce build framework. You do not have to create or implement models yourself.
A full-scale service implementation can contain all of the following architectural aspects:
-
Model definition (required and automatically generated)
-
Actual service implementation (required)
-
DAOs (optional, but recommended)
-
DTOs (optional, but recommended)
-
Facades (optional, but recommended)
-
Strategies (optional, but recommended)
-
Converters (optional, but recommended)
-
Interface definitions backing the service implementation (optional, but recommended)
Using Spring
The SAP Commerce ServiceLayer uses the traditional way to define Spring beans via XML configuration files but optionally there are other ways for bean definitions. This section sums up the alternatives and their pros and cons.
Annotations
Spring 2.5 and later versions allow defining Spring beans via Annotations.
Beans written by SAP Commerce do not define Spring beans using annotations, but use an explicit set of configuration files instead. That way, you can tell at once which beans are available for you to use and do not have to weed through all the Java classes.
Autowiring
Usually you define bean dependencies like this:
<property name="productService" ref="productService"/> <property name="priceService" ref="priceService"/> <property name="productConverter" ref="productConverter"/>
But you can also use Spring autowiring capability. Autowiring is a way to automatically inject dependencies, for example by matching the name of the property with the name of the bean to be injected.
This feature can speed up development because you do not have to explicitly specify all of your dependencies. But at the same time it makes your components more fragile: it can be hard to track down injection errors (which may be caused by typos in a property of bean ID, for example).
If you define dependencies explicitly, the Spring container checks the dependencies on startup and throw an error if it can’t resolve the dependencies. In addition, tools like Eclipse are able to notify you of errors during development.
Also, autowiring limits your flexibility. To have dependencies injected by name or type, you have to make sure that names and types always match and can’t come up with other patterns to construct your beans. For services, you want to maintain maximum stability and flexibility. You may consider using autowiring for frontend components like controllers. There you should build on a solid foundation with a consistent bean naming scheme and can safely have the dependencies injected by name.
Beans written by SAP Commerce do not use autowiring to ensure that only explicitly referenced beans are injected.
Accessing Field Members
Field members of Abstract Services should be private (and therefore accessed with getters and setters).
public abstract class SomeAbstractService extends AbstractService{
private ModelService modelService; // Member fields should be private and accessed with getters, setters
Decorating a Service
Building a Decorator to an existing service can be a good idea if you want to establish a connection less dependent than extending the service. By creating a Decorator, you can virtually extend or override a service implementation without actually touching the original service. That way, you have fewer dependencies on the original service implementation and keep your code separated.
For more information, see How to Extend a Service.
An implementation could look like this:
public class MyDefaultProductServiceImpl extends AbstractBusinessService implements ProductService
{
private ProductService originalProductService;
public ProductModel getProductForCode(final String productCode) throws ProductNotFoundException
{
// Your code here
return originalService.getProductForCode(productCode);
}
// Rest of the methods
}
By delegating to the original service, you keep following the principle of Interface Driven Design. You do not have to make any assumptions on the details of the ServiceLayer implementation. While SAP Commerce guarantees the API to be stable, the underlying implementation may vary. The name of the implementation class or the way it works can change from release to release, which may result in breaking a dependent service implementation.
Strategies
One of the main goals of the ServiceLayer is to enable developers to easily extend existing functionality. While it would be possible to replace a whole service class implementation, this would not be a flexible approach. There are several drawbacks:
-
You have to replace a whole class just to override one method.
-
What do you do when several extensions want to modify the same logic?
A solution is to have the Business Services delegate to finer-grained components if necessary. This approach is known as Strategies. Such a Strategy can look like this:

These Strategies are unique to every use case. Each Strategy may use an individual interface (Interface-Driven-Design).
There can be numerous ways to adapt these Strategies. One way might be to aggregate several Strategies to a composite Strategy like this:
Implementing a Strategy
Strategies are optional. For simple use-cases or very project specific use-cases, simply implementing the services is sufficient.
For example, the CartService:
<alias alias="cartService" name="defaultCartService"/> <bean id="defaultCartService" class="de.hybris.platform.order.services.impl.DefaultCartService" parent="abstractBusinessService" > <property name="addToCartStrategy" ref="addToCartStrategy"/> <property name="cartDao" ref="cartDao"/> <property name="orderCalculation" ref="orderCalculation"/> <property name="cartFactory" ref="cartFactory"/> <property name="sessionService" ref="sessionService"/> </bean>
You see that this implementation depends on an addToCartStrategy, an orderCalculation, and a cartFactory. These strategies perform distinct tasks. So, perhaps it is a better idea to extend these instead of the service on top of them.