Replies: 1 comment 3 replies
-
I believe your problem is the opposite: You are trying to introduce unnecessary decoupling. Providers already act as a decoupling. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In a short conversation here: https://twitter.com/remi_rousselet/status/1280123649400471553?lang=en you discussed that you didn't like the idea of Provider + get_it and that Riverpod should solve that.
I am working on my first project with Riverpod and am starting to be drawn to adding something like get_it with Riverpod. The use of global variables to define the provider can actually introduce unwanted coupling. But it is perhaps hard to explain.
So I try to practice a very clean design with no unnecessary coupling. One version of clean design has the domain layer define repository interfaces. The data layer provides implementation of those interfaces. So the data layer needs to say that I need someone to provide me with an implementation of this interface, but it should in no way be coupled to who provides the implementation of the interface. So the question becomes where and how do you define the provider.
One would like to define the provider inside the domain module. So say the interface was the FooRepository, somewhere in the domain library you would like to say:
But what do you put on the right side of that? If you reference the data module there, then you have broken clean design as the domain module cannot have any coupling to the data module. You could move that provider out of the domain library to your app, but then your domain library has a dependency on files in your app which ones again destroys the clean, uncoupled design.
One alternative I have thought about is that the domain layer declares it like this:
And then in an initialization routine the data module says:
Which works but seems awfully hacky and verbose.
The advantage of DI like in get_it is that you can have code that says I need this dependency, code that creates the implementation, and then binding code that says what implementation is bound to supplying the dependency. And there is no coupling from the needer of the dependency to the supplier of the dependency. That wiring is done in the binding code that is the only one that knows of the existence of the other.
Using global variables ties those 3 pieces into one variable and there is no separation unless you go to extreme manual methods. With DI the needer of the dependency only says I need something of this type and possibly a tag if there is more than one. Without that it is too tightly coupled.
Sure, most people are not as pedantic about such things as me and I am not sure how I am going to work around this yet, but wanted to put the thought out there. Wondering now about the possibility of creating a GetItProvider where the create function merely delegates to get_it to get the instance of the provider
Beta Was this translation helpful? Give feedback.
All reactions