Created
April 12, 2012 00:11
-
-
Save jbosse/2363648 to your computer and use it in GitHub Desktop.
Hack around Ninject 2 => 3 api change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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