should AppEnvironment contain all dependencies from all subvEnvironments? #1105
-
Lets say we have View that creates a new Store (deep in the hierarchy). Should the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Generally, you should hold on to all of your app's dependencies in the root If you don't do this, you can make it harder to control your environment in tests - for example, whilst you can always directly test any feature domain and inject your dependencies using a For example: struct ParentFeature {
var child: ChildFeature
}
enum ParentAction {
case child(ChildAction)
}
struct ParentEnvironment {
var foo: FooClient
}
let fooReducer = Reducer<...>.combine(
childReducer.pullback(
state: \.child,
action: /ParentAction.child,
environment: { _ in ChildEnvironment(bar: BarClient()) }
)
)
// child domain omitted If you want to write some tests for this feature that also exercise parts of the child domain, you have no way of controlling child feature's dependencies because the child environment is hardcoded to use If instead |
Beta Was this translation helpful? Give feedback.
-
@lukeredpath is totally right, though you can use @tgrapperon's library to simplify the boilerplate of dependency passing. We've also been working towards a model that will simplify this (which both Luke and Thomas have some experience with). It's not quite ready yet, but we're hoping to refine it for production in the coming months. |
Beta Was this translation helpful? Give feedback.
Generally, you should hold on to all of your app's dependencies in the root
AppEnvironment
and pass them down the hierarchy to the child domains that need them.If you don't do this, you can make it harder to control your environment in tests - for example, whilst you can always directly test any feature domain and inject your dependencies using a
TestStore
, there are sometimes cases where you need to test a feature domain's integration into a parent domain - when you do this you only have control over the parent domain's environment and not the direct child environment, as this would be created by thepullback
that integrates the child reducer into your parent.For example: