- Hands-On Microservices with Kubernetes
- Gigi Sayfan
- 254字
- 2021-06-24 13:46:37
Employing interfaces and contracts
Interfaces are one of the best tools a software engineer can use. Once you expose something as an interface, you can freely change the implementation behind it. Interfaces are a construct that's being used within a single process. They are extremely useful for testing interactions with other components, which are plentiful in microservice-based systems. Here is one of the interfaces of our sample application:
type UserManager interface {
Register(user User) error
Login(username string, authToken string) (session string, err error)
Logout(username string, session string) error
}
The UserManager interface defines a few methods, their inputs, and outputs. However, it doesn't specify the semantics. For example, what happens if the Login() method is called for an already logged-in user? Is it an error? Is the previous session terminated and a new session created? Is it returning the existing session without an error (idempotent approach)? These kinds of questions are answered by contracts. Contracts are difficult to specify fully and Go doesn't provide any support for contracts. But, contracts are important and they always exist, even if only implicitly.