Is it possible to implement dependency injection without using service locator at the start of an application?
You misunderstand what a Service Locator is. You do understand the part that it is an anti-pattern, which is good, but what you're missing is that the pattern is not about the mechanics, but the role it plays in the application. In other words:
A DI container encapsulated in a Composition Root is not a Service Locator - it's an infrastructure component.
There is nothing inherently wrong with calling the class encapsulating the DI container bootstrapping code ServiceLocator
, but you could also call it a Startup
, Bootstrap
or ContainerWrapper
, it is just a naming convention.
On the other hand ServiceLocator
as a design pattern is usually considered an anti-pattern since it becomes a hard dependency for the rest of the code and makes changes and testing hard and unpredictable. In your code it is Resolve<T>
method which you would want to stay away from to avoid the consequences.
https://en.m.wikipedia.org/wiki/Service_locator_pattern
And to answer your question, a piece of code is usually required to initialize the DI container in any case even when it is hidden from you as part of a bigger DI framework itself, some frameworks though allow configuring your container from the configuration file too. Hope it helps!