fx icon indicating copy to clipboard operation
fx copied to clipboard

feat(fx): add Transient provider option for non-cached constructors

Open rezamokaram opened this issue 2 months ago • 2 comments

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.

rezamokaram avatar Nov 06 '25 23:11 rezamokaram

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 06 '25 23:11 CLAassistant

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

rezamokaram avatar Nov 08 '25 18:11 rezamokaram