7 Service implementation patterns for microservices

 

This chapter covers

  • How hexagonal architecture helps us design loosely coupled services
  • Implementing the business layer for a microservice and implementing database models using SQLAlchemy
  • Using the repository pattern to decouple the data layer from the business layer
  • Using the unit of work pattern to ensure the atomicity of all transactions and using the dependency inversion principle to build software that is resilient to changes
  • Using the inversion of control principle and the dependency injection pattern to decouple components that are dependent on each other

In this chapter, we’ll learn how to implement the business layer of a microservice. In previous chapters, we learned how to design and implement REST APIs. In those implementations, we used an in-memory representation of the resources managed by the service. We took that approach to keep the implementation simple and allow ourselves to focus on the API layer of the service.

In this chapter, we’ll complete our implementation of the orders service by adding a business layer and a data layer. The business layer will implement the capabilities of the orders service, such as taking orders, processing their payments, or scheduling them for production. For some of these tasks, the orders service requires the collaboration of other services, and we’ll learn useful patterns to handle those integrations.

7.1 Hexagonal architectures for microservices

7.2 Setting up the environment and the project structure

7.3 Implementing the database models

7.4 Implementing the repository pattern for data access

7.4.1 The case for the repository pattern: What is it, and why is it useful?

7.4.2 Implementing the repository pattern

7.5 Implementing the business layer

7.6 Implementing the unit of work pattern

7.7 Integrating the API layer and the service layer

Summary