Skip to content

Instantly share code, notes, and snippets.

@jbosse
Created April 12, 2012 00:11
Show Gist options
  • Save jbosse/2363648 to your computer and use it in GitHub Desktop.
Save jbosse/2363648 to your computer and use it in GitHub Desktop.
Hack around Ninject 2 => 3 api change
using System.Linq;
using Hudoo.Web.App_Start;
namespace Hudoo.Web.Test.DeleporterHelpers
{
public static class NinjectControllerFactoryUtils
{
public static void TemporarilyReplaceBinding<TService>(TService implementation)
{
// get a handle on the Ninject Kernel (how you do this will depend on how you setup the Kernel)
var kernel = NinjectWebCommon.Kernel;
// Remove existing bindings and replace with new one
var originalBindings = kernel.GetBindings(typeof(TService)).ToList();
foreach (var originalBinding in originalBindings)
kernel.RemoveBinding(originalBinding);
kernel.Bind<TService>().ToConstant(implementation);
var replacementBinding = kernel.GetBindings(typeof (TService)).ToList()[0];
// Clear up by doing the reverse
TidyupUtils.AddTidyupTask(() =>
{
kernel.RemoveBinding(replacementBinding);
foreach (var originalBinding in originalBindings)
kernel.AddBinding(originalBinding);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment