5

I was wondering if Simple Injector has an option to stop throwing exceptions whenever GetInstance(Of TService) returns Nothing? It appears to be throwing them now because I have two requests to get an instance, it's not there, and it throws the exception.

Is there a way to prevent the default behavior, a setting somewhere, or something else?

Steven
  • 166,672
  • 24
  • 332
  • 435
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Are you sure returning `Nothing` is a valid situation? It is often better return a [Null Object](http://en.wikipedia.org/wiki/Null_Object_pattern) instead of returning `null`. This can also save you from having to call the container from within your application. – Steven Apr 09 '12 at 16:53
  • Yes, in this scenario, Nothing is best to be returned because a follow up statement checks and loads from another source. This is defined within a third-party toolset, so I have no control over that conditional logic. – Brian Mains Apr 09 '12 at 18:07

1 Answers1

15

There absolutely is a simple way of doing this. The SimpleInjector.Container implements System.IServiceProvider, which defines an object GetService(Type) method. This method returns null when a type is not registered. The IServiceProvider however, is implemented explicitly, which means it doesn't show up under normal use, so you have to cast the Container to IServiceProvider, as shown below:

IServiceProvider provider = container;
object instance = provider.GetService(typeof(MyClass));

Or you can define an extension method on top of this:

public static bool TryGetInstance<TService>(
    this Container container, out TService instance)
    where TService : class
{
    IServiceProvider provider = container;
    instance = (TService)provider.GetService(typeof(TService));
    return instance != null;
}

I must admit that this feature is a bit hidden. The only place this method is currently explained is in the reference library.

Do note that in general it is much better to register Null Object Pattern implementations (empty implementations without any behavior) instead of calling an TryGetInstance method. Injecting Null Objects prevents the application from having to worry about null references, which makes your application code easier to understand and easier to test.

rexcfnghk
  • 14,435
  • 1
  • 30
  • 57
Steven
  • 166,672
  • 24
  • 332
  • 435
  • Is there any updates on this in the new versions? Also the link to the reference library is broken and I couldn't find the new one. – mrmashal Jan 23 '16 at 19:23
  • @mrmashal nothing has changed in v3 this respect. – Steven Jan 23 '16 at 23:09
  • Upvote for suggesting registering NULL if not needed. Simply, if you inject in a constructor, then make the parameter optional ( = null) – Alex Klaus Mar 19 '19 at 01:54
  • Hi Alex, I'm afraid you misinterpreted my answer. A Null Object is a design pattern which means an empty implementation. You should not make constructor arguments for injected services optional and never allow `null` to be injected. That leads to extra complexity in the consumer. Instead the registration and injection of a dummy implementation (the Null Object pattern) provides a better solution. – Steven Mar 19 '19 at 07:37