Are Ambients a Service Locator / Dependency Injection API? I haven’t seen official documentation describing them as such, and I think that’s pretty much what it is. I could achieve the same thing with Dagger/Hilt, though the dependencies are scoped within the Composable Hierarchy, instead of Activities/Fragments/Application/ViewModel
10/20/2020, 12:52 AM
They are a service locator. Basically Context.getSystemService
Leland Richardson [G]
10/20/2020, 2:30 AM
they’re a service locator with the very specific and singular scoping ability of scoping something to a composable sub-hierarchy. With the additional caveat that the API also gives you the tools to change the value over time, and have the usage sites will recompose as a result.
I think service locator is a reasonable way to think about it, but i think it’s useful to not overlook the differences, some of which were the primary motivations of the API.
If Dagger/Hilt does the thing you need, then you should probably use that instead of Ambients.