Can I pass constructor parameters to Unity's Resolve method?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, Unity can accept runtime constructor values during resolution, but not by giving Resolve a normal positional argument list. The usual approach is to pass override objects such as ParameterOverride or redesign the registration so the container stays responsible for object construction.
Why Resolve Does Not Look Like new
Dependency injection containers try to centralize object creation rules. If Resolve accepted arbitrary constructor arguments the same way new does, composition logic would spread across the codebase and become harder to reason about.
That is why Unity expects you to register types first and then optionally supply explicit override instructions when one constructor argument must be chosen at runtime.
Use ParameterOverride for Runtime Scalar Values
The most common case is a constructor that takes both injected services and one runtime value such as a connection string, tenant id, or file path. Unity supports that with ParameterOverride.
Here Unity still resolves ILogger from the container, but the region constructor argument is supplied at the call site.
The parameter name matters. ParameterOverride("region", "ca-east") matches the constructor parameter by name, not just by type.
Use DependencyOverride for a Specific Dependency Instance
Sometimes the runtime value is not a primitive or string. Instead, you want to replace one dependency with a specific instance for a particular resolution. That is what DependencyOverride is for.
This is useful in tests, temporary workflows, or background jobs where one dependency differs from the normal application registration.
Factory Registrations Are Often Cleaner
If you find yourself passing overrides repeatedly, a factory is usually a better design. The factory keeps the container wiring in one place and makes the runtime input explicit.
This approach usually reads better than scattering ParameterOverride calls all over the application.
Another Option: Register the Constructor Rule
If the value is not truly dynamic and should always be the same for a given registration, do not pass it at resolve time. Register it as part of the container setup instead.
This is appropriate when the parameter is configuration, not per-request data.
Common Pitfalls
- Expecting
Resolveto accept normal constructor arguments by position. - Misspelling the constructor parameter name in
ParameterOverride. - Passing many overrides and effectively rebuilding the object graph outside the container.
- Using resolve-time overrides for stable configuration that belongs in registration.
- Replacing dependencies casually without thinking about lifetime and disposal behavior.
Summary
- Unity does support runtime constructor values, but through override objects rather than a plain argument list.
- Use
ParameterOverridefor scalar constructor inputs such as strings and ids. - Use
DependencyOverridewhen one resolved dependency must be replaced for a single call. - Prefer a factory registration if the same override pattern happens repeatedly.
- Keep stable configuration in container registrations, not at every resolve call.

