Remove Modular.get() - Service locator (Anti-Pattern) #638
Replies: 4 comments
-
Hi, @AlvaroVasconcelos. I agree that service locator has many problems. It's actually a trade-off. It facilitates dependency inversion with the cost of being inherently unsafe. In the context of Flutter we have some alternatives:
Maybe I am missing some options, but this is the most obvious for me. In the case of Modular and the way it works I don't think one of this options is viable, but it's obviously open to discussion. Do you have anything in mind as a viable alternative to the way it's done today? |
Beta Was this translation helpful? Give feedback.
-
Has a good discution here (almost 8 years ago, so dis discution is not anything new): |
Beta Was this translation helpful? Give feedback.
-
My point of view is: you don't like it, so you don't use it. |
Beta Was this translation helpful? Give feedback.
-
Hi! (Service Locator violates SOLID)
(Service Locator violates encapsulation)
Those are my points about. I would love to see more comments on the subject. |
Beta Was this translation helpful? Give feedback.
-
Service Locator is it's actually an anti-pattern and should be avoided.
the problem with Service Locator is that it hides a class' dependencies, causing run-time errors instead of compile-time errors, as well as making the code more difficult to maintain because it becomes unclear when you would be introducing a breaking change.
Service Locator violates SOLID:
Violates the Interface Segregation Principle (ISP). That's because a Service Locator effectively has infinitely many members.
Service Locator violates encapsulation:
Violates encapsulation in statically typed languages because it doesn't clearly communicate preconditions.
Beta Was this translation helpful? Give feedback.
All reactions