How can I create an @Injected for an instance with parameters #275
Replies: 2 comments
-
|
Updates from my findings: I cannot use a container, because it has to be I’m using: Example of resolution logic: My Even though: Factory sometimes creates a new instance for the same scope+key pair. Logging shows that the I tried promoting the factory to a stored property (e.g. ✅ Temporary workaround I’m currently working around this by bypassing Factory and manually caching the instance... This guarantees the behavior I expect, but I’d love to understand what might be going wrong with Factory’s scoped resolution, or if there’s something I’m missing in how scope caching works. ⸻ Let me know if I should try to provide a minimal reproducible example — happy to dig deeper! Thanks 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
Just added.... ParameterFactory ScopesBy default, ParameterFactory scopes will cache the first requested value and then return the service created with that value, even if other values are passed on future requests. The behavior can be changed with the var parameterService: ParameterFactory<Int, ParameterService> {
self { ParameterService(value: $0) }.scopeOnParameters.cached
}The passed parameter must be Hashable for this modifier to appear and for the per-parameter caching functionality to occur. That code is in 2.5.0. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! First of all, thanks for the great work on Factory — it’s been a pleasure to use 🙌
I’m running into a design question and would love your input.
Context
I have a struct that looks like this:
This
Dependencyis used in multiple places, for example:I’m displaying a list of previews, and each preview has a StateManager. This state manager is tied to the context of the preview and must not be shared.
This is not answered by
ParameterFactorysince it would resolve the first call and ignore other calls to it with different parameters. Also, this is different since I need a difference instance ofDependencyfor eachspecificProperty.My current approach
I’m using Factory with scoped containers and passing parameters:
• I create a scope for each preview.
• I store those scopes in a dictionary using a unique ID.
• I resolve dependencies on the fly from the scope.
This mostly works, but I occasionally observe that a dependency gets resolved again instead of reusing the existing one. It’s rare (~10% of the time) but problematic, since the state manager is expected to persist per preview. It might be a race condition, but it highlights that my use of
Factoryis not the best.Questions
Would love any insight or examples of how to properly model this scenario using Factory!
Thanks in advance 🙏
Beta Was this translation helpful? Give feedback.
All reactions