How do you work with contracts during the development of a new service when contracts are not finalized and often change?
sobes.tech AI
Answer from AI
When working with changing contracts during the development of a new service, it is useful to employ the following approaches:
-
Versioning of contracts. Create versions of APIs or contracts to support multiple options simultaneously and gradually migrate clients.
-
Using mocks and stubs. Until the contract is finalized, work with mock implementations that are easy to update when changes occur.
-
Contract testing. Tools like Pact allow verifying the compliance between services even when changes happen.
-
Frequent communication with the team. Quick feedback and discussion of changes help avoid misunderstandings.
-
Flexible architecture. Design the service so that changes in the contract minimally affect the internal logic.
Example: In Go, interfaces and mock structures can be used, which are easy to change when contracts change.
// Service interface
type BookService interface {
GetBook(id string) (*Book, error)
}
// Mock for testing
type MockBookService struct {
GetBookFunc func(id string) (*Book, error)
}
func (m *MockBookService) GetBook(id string) (*Book, error) {
return m.GetBookFunc(id)
}
This approach allows quick adaptation to contract changes without a complete overhaul of the code.