-
Hey all, I'm working on a project and I started doing the following, but I'm not sure if that is safe to do and what the behavior of it. extension DependencyA: DependencyKey {
static var liveValue: DependencyA {
@Dependency(\.dependencyB) var dependencyB
return DependencyA {
dependencyB.doSomething()
}
}
} What's the correct way of accessing dependencies inside other dependencies? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Yes, that is completely fine to do. We have an example of doing this in order to define the live database client for www.pointfree.co, which needs access to environment variables loaded from Heroku. One thing to keep in mind is that currently the library does not do any circular dependency detection, so if through some chain of events |
Beta Was this translation helpful? Give feedback.
-
Let's say I'm writing a test for a Model that depends on extension DependencyA: DependencyKey {
static var liveValue: DependencyA {
@Dependency(\.dependencyB) var dependencyB
return DependencyA {
dependencyB.doSomething()
}
}
}
struct MyModel {
var myBool: Bool = false
@Dependency(\.dependencyA) var dependencyA: () -> Bool
mutating func generateTrue() {
self.myBool = dependencyA()
}
}
func testMyModel() {
let myModel = withDependencies {
} operation: {
MyModel()
}
myModel.generateTrue()
XCTAssertTrue(myModel.myBool)
} In this test, I'd see an |
Beta Was this translation helpful? Give feedback.
Yes, that is completely fine to do. We have an example of doing this in order to define the live database client for www.pointfree.co, which needs access to environment variables loaded from Heroku.
One thing to keep in mind is that currently the library does not do any circular dependency detection, so if through some chain of events
DependencyB
starts depending onDependencyA
, you will get a crash. We may improve this later but it is not a priority right now.