https://github.com/firasdarwish/ore
Lightweight, generic & simple dependency injection (DI) container for Go
https://github.com/firasdarwish/ore
Last synced: 9 months ago
JSON representation
Lightweight, generic & simple dependency injection (DI) container for Go
- Host: GitHub
- URL: https://github.com/firasdarwish/ore
- Owner: firasdarwish
- License: mit
- Created: 2024-02-17T17:06:52.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T12:44:03.000Z (about 1 year ago)
- Last Synced: 2024-10-31T09:49:15.994Z (about 1 year ago)
- Language: Go
- Size: 48.8 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - ore
- awesome-go-plus - ore - Lightweight, generic & simple dependency injection (DI) container.  (Miscellaneous / Dependency Injection)
- awesome-go - ore - Lightweight, generic & simple dependency injection (DI) container. (Miscellaneous / Dependency Injection)
- awesome-go-with-stars - ore - Lightweight, generic & simple dependency injection (DI) container. (Miscellaneous / Dependency Injection)
- awesome-go - ore - Lightweight, generic & simple dependency injection (DI) container. (Miscellaneous / Dependency Injection)
- awesome-go - firasdarwish/ore
- awesome-go - ore - Lightweight, generic & simple dependency injection (DI) container. (Miscellaneous / Dependency Injection)
- fucking-awesome-go - ore - Lightweight, generic & simple dependency injection (DI) container. (Miscellaneous / Dependency Injection)
- awesome-go-cn - ore
README
# Ore: A Lightweight Dependency Injection Container for Go
[](https://pkg.go.dev/github.com/firasdarwish/ore)
[](https://goreportcard.com/report/github.com/firasdarwish/ore)
[](https://github.com/avelino/awesome-go?tab=readme-ov-file#dependency-injection)
[](https://codecov.io/gh/firasdarwish/ore)

**Ore** is a powerful and flexible **Dependency Injection (DI)** library for Go, designed to simplify complex
application structures while maintaining performance and modularity.
# 🌐 **[Full Documentation: ore.lilury.com](https://ore.lilury.com)**
---
## Key Features
### 1. **Flexible Lifetime Management**
- **Singletons**: Lifetime spans the entire application.
- **Scoped**: Lifetime is tied to a specific context.
- **Transient**: New instance created on every resolution.
---
### 2. **Alias Support**
- Link multiple implementations to the same interface.
- Easily resolve the preferred implementation or retrieve all registered options.
---
### 3. **Graceful Termination**
- **Application Termination**: Shutdown all resolved singletons implementing `Shutdowner` in proper dependency order.
- **Context Termination**: Dispose all resolved scoped instances implementing `Disposer` when the context ends.
---
### 4. **Placeholder Registration**
- Register incomplete dependencies at application setup.
- Provide runtime values dynamically when requests or events occur.
---
### 5. **Multiple Containers (Modules)**
- Create isolated dependency graphs for modular applications.
- Enforce module boundaries by separating service registrations and resolutions per container.
---
### 6. **Advanced Dependency Validation**
- Detect and prevent common pitfalls like:
- **Missing Dependencies**: Ensure all resolvers are registered.
- **Circular Dependencies**: Avoid infinite loops in dependency resolution.
- **Lifetime Misalignment**: Catch improper lifetime dependencies (e.g., singleton depending on transient).
---
### 7. **Keyed and Keyless Registration**
- Support for multiple instances of the same type, scoped by keys.
- Easily resolve services using keys to manage module-specific configurations.
---
### 8. **Runtime Value Injection**
- Inject dependencies dynamically during runtime, tailored to request context or user data.
---
### 9. **Context-Based Dependency Resolution**
- Dependencies can be tied to a specific `context.Context`.
- Automatic cleanup of scoped services when the context ends.
---
## Installation
```bash
go get -u github.com/firasdarwish/ore
```
### Quick Example
```go
package main
import (
"context"
"fmt"
"github.com/firasdarwish/ore"
)
// Define a service
type MyService struct {
Message string
}
func main() {
// Register a singleton service
ore.RegisterSingleton[SomeServiceInterface](&MyService{Message: "Hello, Ore!"})
// Resolve the service
service, _ := ore.Get[*SomeServiceInterface](context.Background())
fmt.Println(service.Message) // Output: Hello, Ore!
}
```
For complete usage examples and advanced configurations, visit the **[Ore Documentation](https://ore.lilury.com)**.
## 👤 Contributors

## Contributing
Feel free to contribute by opening issues, suggesting features, or submitting pull requests. We welcome your feedback
and contributions.
## License
This project is licensed under the MIT License - see the LICENSE file for details.