Thursday, December 17, 2015

Dependency Injection vs Service Locator



Dependency Injection and Service Locator, two different ways to implement Inversion of control principle.

A dependency injector container acts as an assembler of objects, decoupling your business logic from their dependencies. But sometimes is more complex to debug when we have a issue.

On the other hand, Service Locator hides the implementation details too, but your class has dependency with the Service Locator class. Only, when this dependency is not a problem, the Service Locator pattern is a good choice.

Most important of all, is to split the configuration code from the use code. Take it into account , and  analyze when one option is better than the other.

Until next post!

No comments:

Post a Comment