The ServiceLayer
When implementing new business logic, you separate the business code into java classes called services. Each service implements a specific, well-defined requirement.
Services form part of the ServiceLayer. This layer is a logical tier between the client and persistence layer. Each service has its own java interface that lists the public methods that can be called by clients and other services.
A service may contain all required business logic, but more commonly will delegate to one or more of the following:
- Other services providing part of the behavior required
- Strategy objects that provide swappable behavior for different requirements, e.g. different cart calculation algorithms
- Data access objects that handle locating and retrieving data items from the database
These other services, strategy objects and data access objects are also defined as Spring beans, with respective interfaces and implementations.
To build your service, you create the following files in the src folder of the concerttours extension:
- BandDAO: The DAO interface, which describes the CRUD functionality that you require, in this case only read functionality.
- DefaultBandDAO: The DAO implementation
- BandService: The band service interface
- DefaultBandService: The band service implementation
Extend the ServiceLayer
Add new functionality to your extension to list and find Bands by adding a new service interface, service implementation, and supporting DAOs.
Prerequisites
public void testServiceLayerClassesExist() throws IOException {
// If you have correctly added an extension there should be some new folders and files
assertTrue("You should have added concerttours.daos.BandDAO.java", FileHelper.fileExistsAndContains(
"../hybris/bin/custom/concerttours/src/concerttours/daos/BandDAO.java", "public interface BandDAO"));
assertTrue("You should have added concerttours.daos.impl.DefaultBandDAO.java", FileHelper.fileExistsAndContains(
"../hybris/bin/custom/concerttours/src/concerttours/daos/impl/DefaultBandDAO.java", "public class DefaultBandDAO implements BandDAO"));
assertTrue("You should have modified concerttours-spring.xml", FileHelper.fileExistsAndContains(
"../hybris/bin/custom/concerttours/resources/concerttours-spring.xml", "<context:component-scan base-package=\"concerttours\"/>"));
assertTrue("You should have added concerttours.service.impl.DefaultBandService.java", FileHelper.fileExistsAndContains(
"../hybris/bin/custom/concerttours/src/concerttours/service/impl/DefaultBandService.java", "public class DefaultBandService implements BandService"));
assertTrue("You should have added concerttours.service.BandService.java", FileHelper.fileExistsAndContains(
"../hybris/bin/custom/concerttours/src/concerttours/service/BandService.java", "public interface BandService"));
}
Before you begin this step, run the test to confirm that it fails by executing this command:
cd $HYBRIS_HOME_DIR/hybris123; mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testServiceLayerClassesExist test
cd %HYBRIS_HOME_DIR%\hybris123 & mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testServiceLayerClassesExist test