Can't seem to use/find SimpleInjector.Extensions?

Jan 19, 2015 at 10:48 PM
I am new to SimpleInjector and just tried installing and creating a sample program.
I used nuget to install SimpleInjector, v2.7.0.

I am trying to use with log4net and am attempting to use
container.RegisterWithContext

However, the documentations says to include
using SimpleInjector.Extensions

but for the life of me, I can't seem to get this to work.
Is there another nuget package for SimpleInjector.Extensions? I don't see this dll anymore.

Thanks.
Coordinator
Jan 20, 2015 at 6:27 AM
The Context Based Injection section in the documentation refers to Context Dependent Extensions code snippet in the appendix. In other words, this extension method is not part of the API; instead it is given as example code in the documentation.
Jan 20, 2015 at 4:31 PM
Thank you.
You might want to update the documentation -- at the top of the 'advanced scenarios' page, it talked about adding a reference to namespace "SimpleInjector.Extensions", and so I thought it was included as part of the API or as part of another DLL / nuget package. I got it to work finally, and also, with some code samples of using log4net here:

For others reading, I also got log4net working using the example from this forum:
http://forums.whirlpool.net.au/archive/2165746
container.RegisterWithContext<ILog>(dc =>
    {
      if (dc.ImplementationType == null)
      {
        return LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
      }
      return LogManager.GetLogger(dc.ImplementationType);
    }
  );
Coordinator
Jan 21, 2015 at 10:26 AM
Hi chanra,

Good point. We will update the documentation to make this more clear.

Interesting forum post btw. If I was aware of this, I would have anticipated in this thread, because I don't always agree about everything that is said there. Short summary:
  • Hiding log4net behind your own abstraction is actually a quite good idea, because you should minimize dependencies on external frameworks as much as possible, and besides that, log4net's ILog interface is a hideous interface. Also read this.
  • Having one logger per class is an indication that you are missing a generic cross-cutting concern that can be applied to a wide range of classes. One of the comments already refers to this Stackoverflow answer (of mine). I think it would be good to read it.
  • The commenters seem to agree that Resharper can be used to generate code, but not having to type code has never really been a solution to a problem. The problem is maintainability and letting Resharper generate thousands of similar peaces of code will not help with maintainability. As a matter of fact: it hurts maintainability. And it is still code that makes your class more complex and causes you to write more test code. Instead, again, read this answer.
Do note though that many improvements have been made to Simple Injector since that discussion. For instance, in most cases, you will now be able to minimize given registration to the following:
container.RegisterWithContext<ILog>(dc => LogManager.GetLogger(dc.ImplementationType));
This didn't work in the past, because db.ImplementationType will be null in case ILog is not injected, but requested directly using container.GetInstance<ILog>() and in the past, this is what would happen when container.Verify() was called. Newer versions of Simple Injector however, detect what registrations are dependencies and which are root types (registrations that nobody depends upon). When verifying, Simple Injector will only call GetInstance on root types and this allows the following registration to work, as long as you don't request ILog directly.