IOC versus service locator

Service locator is a design pattern used to locate other services. It is the opposite of DI. Depending on its usage, it can be an antipattern when we move the dependency to the service locator instead of the concrete class.

The other issue with using service locator is that it can often be difficult to change the implementation and to test the system. Taking the opposite approach, IOC builds the dependencies once for each application when the application is launched for first time.

The goal of DI is to inject dependencies during the application initialization. If we call the dependencies later, this becomes a service location.

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.