Skip to content

Service

Anshuman Chhapolia edited this page Mar 23, 2024 · 1 revision

Service

Services are the business layer of the application. They contain the business logic of the application. The service layer is responsible for the communication between the controller and the dao layer. The service layer should only call the dao layer to perform the database operations. This will help in keeping the code clean and maintainable.

@smoke-trees/postgres-backend exponses a Service class that you must extend to get the prebuilt features like create, read, update, delete etc. If you aren't working with database operations I would suggest not extending the Service class.

Here is an example of a service class:

export class AddressService extends Service<Address> {
  dao: AddressDao;

  constructor(dao: AddressDao) {
    super(dao);
    this.dao = dao;
  }
}

The Service class is a generic class that takes the entity type as a parameter. The Service class has methods same as to the DAO classes and simply wrap the DAO methods with no logic at this layer. We expect you override the methods in the service layer to add the business logic necessary for your application.

As a rule we suggest to test the service layer as much as possible as it is the layer that contains the business logic of the application. It will be easy to test as compared to the controller layer that contains the request and response.

Clone this wiki locally