Parsley is a reflection-based dependency injection (DI) package for Go that streamlines dependency management through automated lifetime management, type-safe registration, service resolution and activation, proxy generation, and method interception. It leverages reflection only at startup, ensuring runtime efficiency, and supports features like mocking and testing integration for better development workflows.
Dependency injection in Go typically relies on constructor functions, which are idiomatic and foundational for creating and managing dependencies. While this approach is powerful, it can lead to boilerplate code as projects become more complex, requiring explicit instantiation and manual wiring. Addionally, more sophisticated ramp-up code is needed, if not all instances shall be created at application start. Parsley eliminates much of this repetitive code by automating the creation and wiring of dependencies, enabling developers to focus on application logic. It aims to enhance modularity and make managing dependencies easier.
- Type Registration: Supports constructor functions, lifetime management (singleton, scoped, transient), and safe casts.
- Modular and Lazy Loading: Register types as modules, resolve dependencies on-demand, and inject lazily with
Lazy[T]
. - Registration Validation: Ensures early detection of missing registrations or circular dependencies.
- Advanced Registrations: Register multiple implementations for the same interface using named services or lists.
- Proxy and Mock Support: Generate extensible proxy types and configurable mocks to streamline testing workflows.
For a complete overview of features and capabilities, refer to the Parsley documentation.
Add Parsley to your project:
go get github.com/matzefriedrich/parsley
For advanced features like proxy generation or mock creation, install the parsley-cli
utility:
go install github.com/matzefriedrich/parsley/cmd/parsley-cli
Copyright 2024 - Matthias Friedrich