Injector Configuration
Remy DI provides flexible injector configuration options to customize how dependencies are registered, resolved, and managed. You can create multiple injector instances with different configurations to suit your application’s needs.
The NewInjector function creates a new injector instance. You can optionally pass a Config struct to customize its
behavior:
| |
Type: bool
Default: false
Determines if a bind can be overridden if it’s registered twice. When false, attempting to register the same type
twice (using either Register or Override) will panic. When true, you can override existing registrations.
ℹ️ INFO:
CanOverridemust betrueto use theOverridefunction. Without it, callingOverridewill panic even if you explicitly want to override a dependency.
| |
When CanOverride: false:
| |
Use cases:
- Testing scenarios where you want to replace dependencies
- Development environments where you need flexibility
- Plugin systems where modules can override base dependencies
Type: bool
Default: false
Enables duck typing for element retrieval. When enabled, Remy can discover and return elements that implement the requested interface, even if they weren’t registered with that exact type.
How it works:
When DuckTypeElements is enabled, you can use Get[interfaceName] directly. Remy will internally call GetAll to
search through all registered elements and find matches. If exactly one match is found, it returns that element. If
multiple matches are found, it raises an error.
⚠️ CAUTION: This option is computationally expensive, as it must check all registered elements to find matches.
| |
⚠️ WARNING: If
Getfinds multiple matches, it will return an error:
| |
Use cases:
- Plugin architectures where multiple implementations exist
- Service discovery patterns
- Testing scenarios with mock implementations
- When you want to retrieve by interface without knowing the concrete type
Type: Injector
Default: nil
Creates a child injector that can access all elements registered in the parent injector. The child injector can have its own additional registrations, but the parent cannot access the child’s registrations (scope-safe).
| |
Use cases:
- Modular applications with scoped dependencies
- Request-scoped injectors in web applications
- Testing with isolated dependency scopes
- Plugin systems with base and extension dependencies
Remy provides a global injector that can be used without explicitly passing an injector instance. Pass nil as the
injector parameter to use the global injector.
| |
When to use:
- Simple applications with a single dependency container
- Quick prototyping
- Applications where a single injector is sufficient
You can create sub-injectors from an existing injector using the SubInjector method. This creates a child injector
with optional override configuration.
| |
- Use default configuration for most cases - Remy’s defaults are optimized for common scenarios
- Enable
CanOverrideonly when needed (testing, development) - Use
DuckTypeElementssparingly - it has performance implications - Leverage
ParentInjectorfor modular applications with clear dependency scopes - Use
NewBindEntryorNewBindEntryTaggedinGetWithPairs- The type key is automatically generated from the value’s type - Create sub-injectors for request-scoped or test-scoped dependencies
| Option | Default | Performance Impact | Use Case |
|---|---|---|---|
CanOverride | false | None | Testing, development |
DuckTypeElements | false | High | Plugin systems, service discovery |
ParentInjector | nil | Low | Scoped dependencies |
ℹ️ INFO: Remy uses zero-width generic types for bindings, providing compile-time type safety without requiring reflection. The type key is automatically generated from the value’s type when using
NewBindEntryorNewBindEntryTagged.