Getting Started
Installation
Requirements
Install
Module Support
IDE Support
Next Steps
Core Concepts
Dependency Injection
Services and Constructors
What is a Service?
What is a Constructor?
The Service Collection
The Provider
Service Lifetimes
Singleton
Scoped
Transient
Dependency Resolution
Scopes
Type Safety with Generics
Resource Cleanup
Complete Example
Next Steps
Fundamentals
Service Lifetimes
The Three Lifetimes
Singleton - Application Lifetime
Scoped - Request Lifetime
Transient - Always New
Lifetime Rules
The Golden Rule
Practical Examples
Web Application Pattern
Background Job Pattern
Lifetime and Performance
Memory Usage
Creation Cost
Disposal and Cleanup
Common Mistakes
1. Wrong Lifetime for Database Connections
2. Caching Request Data in Singletons
3. Not Considering Thread Safety
Best Practices
Next Steps
Service Registration
Basic Registration
Constructor Functions
Instance Registration
Constructor Patterns
Interface Return Types
Multiple Return Values
Multiple Services from One Constructor
Registration Options
Named Services (Keyed)
Interface Registration (As)
Lifetime Methods
Validation
Build-Time Validation
Common Registration Errors
Advanced Registration
Conditional Registration
Factory Pattern
Generic Services
Best Practices
Common Patterns
Repository Pattern
Service Layer Pattern
Next Steps
Dependency Resolution
How Resolution Works
The Dependency Graph
Resolution Process
Resolution Methods
Basic Resolution
Keyed Resolution
Group Resolution
Automatic Dependency Injection
Constructor Analysis
Recursive Resolution
Circular Dependencies
Detection
Breaking Circles
Option 1: Lazy Resolution with Provider
Option 2: Setter Injection
Option 3: Refactor Design
Lifetime Validation
The Problem
Why It Matters
Valid Dependency Chains
Resolution Context
Using Scopes
Context Integration
Performance Considerations
Caching Strategy
Build-Time Optimization
Error Handling
Resolution Errors
Best Practices
Next Steps
Scopes & Isolation
Understanding Scopes
What is a Scope?
Scope Hierarchy
Web Application Patterns
HTTP Request Isolation
Request Context Pattern
Concurrent Request Handling
Isolation in Action
Thread Safety
Context Integration
Passing Scopes Through Context
Context Cancellation
Database Transactions
Transaction per Scope Pattern
Unit of Work Pattern
Resource Cleanup
Automatic Disposal
Cleanup Order
Testing with Scopes
Isolated Test Cases
Mock Injection
Best Practices
Common Pitfalls
Forgetting to Close Scopes
Storing Scopes in Singletons
Next Steps
Resource Management
The Disposable Interface
Implementing Disposable
Common Disposable Resources
Cleanup Lifecycle
Disposal Order
Lifetime-Based Cleanup
Resource Pooling
Connection Pool Pattern
Graceful Shutdown
Provider Shutdown
Background Worker Management
Transaction Management
Auto-Rollback Pattern
Memory Management
Preventing Memory Leaks
Resource Limits
Error Handling in Cleanup
Aggregating Disposal Errors
Critical vs Non-Critical Cleanup
Testing Resource Management
Verify Cleanup
Mock Disposables
Best Practices
Common Patterns
Resource Wrapper
Cleanup Chain
Next Steps
Modules
What are Modules?
Creating Modules
Basic Module
Module with Configuration
Module Organization
Layer-Based Structure
Feature-Based Structure
Composing Modules
Nested Modules
Conditional Modules
Module Dependencies
Explicit Dependencies
Testing with Modules
Test Module
Module Isolation
Real-World Example
E-commerce Application
Best Practices
Module Patterns
Plugin System
Environment-Specific Modules
Next Steps
Advanced Features
Keyed Services
Understanding Keyed Services
Registration with Keys
Named Services
Resolution
Common Use Cases
Multiple Database Connections
Strategy Pattern
Environment-Specific Services
Advanced Patterns
Dynamic Key Resolution
Fallback Pattern
Factory with Keys
With Parameter Objects
Using Named Dependencies in Parameter Objects
Multi-Tenant Applications
Tenant-Specific Services
Request-Scoped Tenant Resolution
Testing with Keyed Services
Mock Specific Implementations
Override Specific Keys
Best Practices
Key Constants
Common Pitfalls
Key Conflicts
Type Mismatches
Next Steps
Service Groups
Understanding Service Groups
Registration with Groups
Basic Group Registration
With Interface Registration
Common Use Cases
Middleware Pipeline
Plugin System
Event Handlers
Validation Pipeline
Advanced Patterns
Ordered Groups
Conditional Group Members
Dynamic Group Resolution
With Parameter Objects
Group Dependencies in Parameter Objects
Testing with Groups
Test Specific Group Members
Mock Entire Groups
Best Practices
Group Constants
Empty Group Handling
Common Pitfalls
Mixing Keys and Groups
Type Consistency
Next Steps
Parameter Objects (In)
Understanding Parameter Objects
Basic Usage
Creating Parameter Objects
Field Tags
Optional Dependencies
Handling Optional Fields
Named Dependencies
Using Keyed Services
Group Dependencies
Injecting Service Groups
Complex Examples
Mixed Dependencies
Nested Services
Benefits
1. Cleaner Constructors
2. Easier Refactoring
3. Self-Documenting
Testing with Parameter Objects
Creating Test Parameters
With Test Provider
Best Practices
Good Structure
Common Pitfalls
Named Embedding
Unexported Fields
Pointer vs Value
Next Steps
Result Objects (Out)
Understanding Result Objects
Basic Usage
Creating Result Objects
Field Tags
Common Use Cases
Database Connections
Application Bootstrap
Repository Factory
With Groups
Multiple Handlers
Middleware Collection
Advanced Patterns
Conditional Services
Mixed Registration
Service Initialization
Combining with Parameter Objects
Full Example
Testing with Result Objects
Mock Services
Partial Mocking
Best Practices
Good Grouping
Common Pitfalls
Named Embedding
Nil Fields
Circular Dependencies
Next Steps
Resource Management
The Disposable Interface
Implementing Disposable
Common Disposable Resources
Cleanup Lifecycle
Disposal Order
Lifetime-Based Cleanup
Resource Pooling
Connection Pool Pattern
Graceful Shutdown
Provider Shutdown
Background Worker Management
Transaction Management
Auto-Rollback Pattern
Memory Management
Preventing Memory Leaks
Resource Limits
Error Handling in Cleanup
Aggregating Disposal Errors
Critical vs Non-Critical Cleanup
Testing Resource Management
Verify Cleanup
Mock Disposables
Best Practices
Common Patterns
Resource Wrapper
Cleanup Chain
Next Steps
Quick Links
GitHub
Go Packages
Changelog
Community
Contributing
Discussions
Issues
Quick Links
GitHub
pkg.go.dev
Go Report Card
Community
Discussions
Issues
godi
Index
Edit on GitHub
Index