Why Microsoft.Practices.ServiceLocation.IServiceLocator does not offer TryGetInstance()?
I need to get generic validator instance ServiceLocator.Current.GetInstance<IEntityValidator<TEntity>>() but not all Entities has registered validator.
The only solution i found is to use try{}catch{} block, but i dont like this approach.
I can’t tell you why this method doesn’t exist, but I would like to offer the opion that it shouldn’t matter because you shouldn’t be using DI Containers in a pull-based way in any case. This is Service Locator anti-pattern.
If you need an
IEntityValidator<Foo>, then request the dependency through the constructor:You can handle the issue that not all Entities have registered validators in different ways.
My preferred approach would be to register a Null Validator for all those entities.
Alternatively, you can give your Entities a constructor overload that doesn’t take a validator, and then assign a Null Validator from that constructor. That might look like this:
However, whether that work or not partially depends on your particular DI Container. As an example, Castle Windsor uses the most greedy constructor it can satisfy so in that case it would work well even when validators are not registered, because it would just pick the default constructor.
In any case, the push-based approach is true Dependency Injection. With that approach, you use the DI Container to resolve the entire dependency graph in one go at the application’s entry point.