IoC.Resolve vs Constructor Injection

Date: 2019-12-24

https://stackoverflow.com/questions/2176833/ioc-resolve-vs-constructor-injection

https://stackoverflow.com/a/2179260

Source: https://martinfowler.com/articles/injection.html#UsingAServiceLocator

Concluding Thoughts

The current rush of lightweight containers all have a common underlying pattern to how they do service assembly – the dependency injector pattern. Dependency Injection is a useful alternative to Service Locator. When building application classes the two are roughly equivalent, but I think Service Locator has a slight edge due to its more straightforward behavior. However if you are building classes to be used in multiple applications then Dependency Injection is a better choice.

If you use Dependency Injection there are a number of styles to choose between. I would suggest you follow constructor injection unless you run into one of the specific problems with that approach, in which case switch to setter injection. If you are choosing to build or obtain a container, look for one that supports both constructor and setter injection.

The choice between Service Locator and Dependency Injection is less important than the principle of separating service configuration from the use of services within an application.

30660cookie-checkIoC.Resolve vs Constructor Injection