Search code examples
simple-injector

Is Simple Injector dropping support for CommonServiceLocator?


I was reading this SO question:

SetResolver in console application using Simple Injector

In it, I see this remark:

I fixed this and pushed v2.8.2 of the CommonServiceLocator adapter to NuGet. But note that we are dropping support for the CSL adapter in v3 of Simple Injector because it leads to bad practice. Don't use it; you don't need it.

Just what does this mean?

Does this mean that this code will no longer work?

// configuration
var container = new Container();
InitializeContainer(container);
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

// access
var container = GlobalConfiguration.Configuration.DependencyResolver;
var myObject = container.GetService(typeof(MyObject)) as MyObject;

If so, could I register a vote that this access not be removed? We don't use the service locator pattern often, but we have a couple of critical bits of functionality where we simply don't have any other choice. If we can't use DependencyResolver, we're forced back to using static variables.


Solution

  • Just what does this mean?

    It means that from v3 and beyond we will not support a NuGet package for the Common Service Locator project. That's it. Nothing more; nothing less.

    So this means that if you want to use the Common Service Locator project, you can try using the CommonServiceLocator.SimpleInjector NuGet package for v2.x that is not guaranteed to keep working in newer versions of v3 and beyond. So it might be wiser to write your own adapter.

    What this doesn't mean is that we drop support for Web API, MVC and all other frameworks that use some sort of IDependencyResolver adapter. There are integration packages available and they are still supported. So the code examples here and here are still correct and your code example should work just fine.