Giving applications direct access to the container is considered bad practice. This is the form of a service locator pattern that is considered an anti-pattern :
In short, the problem with the Service Locator is that it hides the dependency class, causing runtime errors instead of compile-time errors, and also make the code more difficult to maintain, as it becomes unclear when you introduce the violation.
Since this is considered bad, Simple Injector does not contain anything like StructureMap ObjectFactory.GetInstance . And, in fact, the author of StructureMap considers removing the ObjectFactory API in the release of the StructureMap structure.
However, nothing prevents you from storing an instance of SimpleInjector.Container in a static field and using this application:
At the root of the composition:
public static void Initialize() { var container = new Container(); InitializeContainer(container); DependencyResolver.SetResolver( new SimpleInjectorDependencyResolver(container));
Thus, there are no restrictions in Simple Injector that prevent you from doing this, but frankly, you are already witnessing one of the reasons why the Service Locator is bad: you switched containers, and now you need to change the application code.
Perhaps at the moment it is easiest to save the container in a static field (as shown in the example above), but please take the time to understand why this template is bad and reorganize this template depending on the dependencies of the injection (and especially the constructor injection) .
Good luck.
Steven
source share