|
|
|
|
|
by shadowmint
4745 days ago
|
|
I would argue the single greatest benefit (possibly, only benefit) that DI provides over other service location options (eg. factories) is that you get a nice means to test independent units of code by providing mock implementations. In dynamic languages with no real 'private' members, the benefit is actually pretty minimal because you can assign mock service instances after the object is created. eg. test_instance = Blah()
test_instance.service = MockService()
vs. test_instance = Blah(MockService())
...which means, there isn't a lot of point in using it, unless you're using an IOC container which gives you some other synergy bonuses like singletons, per thread and per request instances, configuration file driven implementation binding, etc. |
|
Testing is very low down on the list of advantages of a IOC container. And even then, most people that think they're doing testing properly with a IOC container are doing it wrong. Hint: "Re-binding" your container by forcing it to drop an existing service implementation and replacing it with your mock/test implementation is "doing it wrong".
The reason there is always so much discourse around DI and IOC is because very few programmers actually bothered to learn, and grok it, even if they think they already have.