Skip to content

Conversation

@rezamokaram
Copy link

Motivation

Currently, fx.Provide() always registers constructors as singletons, meaning their results are reused for the lifetime of the app.
In some cases, it’s desirable to have fresh instances, for example:

  • Creating a new per-request context object
  • Generating unique IDs or temporary objects
  • Managing short-lived resources that shouldn’t be shared globally

Introduces a new fx.Transient() helper that allows constructors to be invoked each time their dependency is requested, rather than once at application start.

This enables transient or per-request dependency lifetimes similar to scoped services in other DI systems. The Transient wrapper registers a factory function that produces a new instance on each call.

Includes unit tests verifying:

  • Constructor runs multiple times for transient providers.
  • Singleton behavior remains unchanged for regular fx.Provide.

Backward compatibility

No breaking changes — existing Fx applications continue to behave identically.
The new feature is purely additive.

Documentation and usage examples will be added in a follow-up PR after this change is approved.

Introduces a new fx.Transient() helper that allows constructors to be invoked
each time their dependency is requested, rather than once at application start.

This enables transient or per-request dependency lifetimes similar to scoped
services in other DI systems. The Transient wrapper registers a factory
function that produces a new instance on each call.

Includes unit tests verifying:
- Constructor runs multiple times for transient providers.
- Singleton behavior remains unchanged for regular fx.Provide.
@CLAassistant
Copy link

CLAassistant commented Nov 6, 2025

CLA assistant check
All committers have signed the CLA.

@rezamokaram
Copy link
Author

Hi @sywhang, just a friendly ping on my PR (#1280) — whenever you have time to review it would be great. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants