Help defining DependencyKey with parameter dependency #1636
Replies: 3 comments
-
You are declaring a static field depending on an instance var this cannot work. Add static to aParameter decl. |
Beta Was this translation helpful? Give feedback.
-
@WedgeSparda Can you share a more concrete example of the dependency you are trying to model, and in particular what kind of parameters you are using to customize its creation? The current example is a little too abstract to give feedback on. As I mentioned in the discussion you linked to (#1528), dependencies in TCA are considered to be static things that live for the entire lifecycle of the application. There isn't a concept of "creating" them with parameters at some point in the application. They simple exist, always. Instead, you can expose endpoints on the dependency that allow updating certain parameters. Or you can make use of |
Beta Was this translation helpful? Give feedback.
-
Hi @mbrandonw, sure. Our context is a bit weird. Our app is splited in different components (frameworks) and there are some dependencies between them. So, this is a pseudocode example of the type in charge of setting up the component and its dependencies. public final class MyComponent {
let provider: DependencyProvider // This is a type carrying all dependencies (keys, use cases, etc) needed to set up the component
public init(with provider: DependencyProvider) {
self.provider = provider
}
// Here we create this component's use cases, resources, etc using the external dependencies provided by provider
} After @Alex293 response I think I figured out how to accomplished what I need, despide needing to add a bit redundant code since right now we're in the middle of the TCA migration and both dependency injection methods need to coexists. public final class MyComponent {
let provider: DependencyProvider
private static var staticProvider: DependencyProvider!
public init(with provider: DependencyProvider) {
self.provider = provider
Self.dependenciesProvider = provider
}
}
extension MyComponent {
enum AnExternalDependencyServiceKey: DependencyKey {
static var liveValue: AnExternalDependencyService { staticProvider.externalDependencySecret }
}
// More dependencies...
}
extension DependencyValues {
var anExternalDependencyService: AnExternalDependencyService {
get { self[AuthExperience.AnExternalDependencyServiceKey.self] }
set { self[AuthExperience.AnExternalDependencyServiceKey.self] = newValue }
}
// More dependencies...
} Not sure if this is the best approach, but it will improve in the future, once me move completely to @dependency, so we can set as static all variables and functions needed to create the external dependencies. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, we're trying to adopt TCA and ReducerProtocol in our project and found an issue I'm not sure how to approach. We have several dependencies traveling through initializers and the new @dependency approach can help us a lot. Problem is that many of those dependencies need some parameters to be created, as an example:
Doing like this we're getting this error:
So my question is, is this even possible with the current state of DependencyKey?
Should instead inject dependencies into our reducers as initializer parameters?
Is the use of @dependency inside another dependency like shown here the only option?
Thanks for your time.
Beta Was this translation helpful? Give feedback.
All reactions