This is a demonstration of a Go application that utilizes the Wire framework for dependency injection. The application is an HTTP server that exposes string transformations as an API. It accepts requests with parameters such as /?transform=0&message=hello to perform various string transformations.
cmd/hellois the main package which uses other packages and starts a http serverconfigis the package to contain individual configs and combined together the whole app configurationhandlerspackage contains http handlershellopackage contains domain specific transform functionalityserverpackage uses available handlers, configuration to expose a http mux at different end pointsvaletpackage is an example to extend the functionality fromhellopackage
- Wire is employed as a compile-time depenedency generation tool
- Each package contains a
wire.gowhere theinjectorsare listed - Each package contains
providersand wireSets - Using wire, packages can be loosely coupled and easily testable
- Each type declares the interfaces that it depends on and also describes its own behaviour with an interface
- If the dependency is known, it goes as property. If it not known then as a functon argument. The type's dependencies are provided by
wire - Keep dependencies to only what are required
- Consider good(simple and clear) APIs
- Prefer smaller packages
- Use composition to express clean requirements
- Recheck and verify the generated wire
injectors
- https://github.com/google/wire
- https://go.dev/blog/wire
- https://github.com/google/wire/tree/main/docs
- https://en.wikipedia.org/wiki/SOLID
- Thank you