✅ Dependency Inversion Principle, Dependency Injection
I have a huge brain fart, hopefully someone can clear it out. Imagine the following Scenario:
You've created a Service that you wanna inject into the classes that depend on it.
To respect Dependency Inversion principle we wanna achieve lose coupling, so we wanna create an abstraction for our Services. But the issue is there are no members that all my Services have in common, hence we can't really abstract any functionality into a seperated interface.
But we need an interface to achieve louse coupling, also for later unit tests when our Service needs to get mocked.
A common solution to this according to my research is to create a Marker interface. That means we create an empty interface that doesn't contain any members, it's only purpose is to represent an abstraction so that we can achieve lose coupling.
Now here is the issue:
Now the issue is that this service object is pretty much useless as it's type reference doesn't expose any members. So in order to call the members of our Service, we would have to cast to a more concrete type here which negates the whole point of Dependency Inversion.
So what to do now? Take Concrete type reference here and give up on dependency inversion principle? Doesn't seem right to me. But casting our MarkerInterface to more concrete typereference seems even worse.
So what do I do now?
You've created a Service that you wanna inject into the classes that depend on it.
To respect Dependency Inversion principle we wanna achieve lose coupling, so we wanna create an abstraction for our Services. But the issue is there are no members that all my Services have in common, hence we can't really abstract any functionality into a seperated interface.
But we need an interface to achieve louse coupling, also for later unit tests when our Service needs to get mocked.
A common solution to this according to my research is to create a Marker interface. That means we create an empty interface that doesn't contain any members, it's only purpose is to represent an abstraction so that we can achieve lose coupling.
Now here is the issue:
Now the issue is that this service object is pretty much useless as it's type reference doesn't expose any members. So in order to call the members of our Service, we would have to cast to a more concrete type here which negates the whole point of Dependency Inversion.
So what to do now? Take Concrete type reference here and give up on dependency inversion principle? Doesn't seem right to me. But casting our MarkerInterface to more concrete typereference seems even worse.
So what do I do now?