NancyFx/Nancy.Bootstrappers.StructureMap — pull request #23 by smartcaveman (2014-07-24 09:30:57+00:00)
Title: Refactored out the static call to ObjectFactory

I found this bug when I was unable to wasn't able to view the route cache on the default Nancy diagnostics module. It doesn't look like there's any reason making this static call would have been deliberate. Since I haven't seen any side effects from the change other than the Route Cache feature becoming functional in the diagnostics module, I have to assume the original inclusion was an oversight. This should be merged into the main branch and NuGet since anyone who uses the current package will break their app. In addition to the issue with the diagnostics, it also seems like this would at the very least cause some performance degradation during route resolution.

Note: Although, it doesn't really matter in practice for this case, StructureMap's API has an overload for the `Use` DSL method that accepts a `Func` argument. The `IContext` has a `GetInstance` method, which is implemented by the `BuildSession` class. The `BuildSession` is the same mechanism that the `Container` class uses under the hood, so the delegate with the `IContext` argument would actually be a more direct approach. It also necessarily stays within the scope of dependencies, so you wouldn't need to have a local reference to the container like we have here in order to use it. That version would look like `registry.For>().Use((IContext context) => new Func(context.GetInstance))`, which is probably the best option. I didn't want to make unnecessary changes to the code though, so I left it as is except for the issue that I intentionally fixed.