Skip to content

An easy-to-use reflection-based dependency injection package that fits into any Go application.

License

Notifications You must be signed in to change notification settings

matzefriedrich/parsley

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI Coverage Status Go Reference Go Report Card License GitHub go.mod Go version GitHub Release

FOSSA Status FOSSA Status

What is Parsley?

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.

Why use dependency injection in Go?

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.

Key Features

  • 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.

Usage

Install Parsley

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