Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created January 15, 2013 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/4538388 to your computer and use it in GitHub Desktop.
Save akimboyko/4538388 to your computer and use it in GitHub Desktop.
Ninject Contextual Binding for different environments
void Main()
{
// create kernel with conventions
using(var kernel = InitializeKernel())
{
var backend = kernel.Get<IBackend>();
Assert.That(backend, Is.Not.InstanceOfType(typeof(ProdBackend))
.And.InstanceOfType(typeof(TestBackend)));
}
}
private static readonly Func<bool> IsCurrentEnvironmentProduction =
() => Environment.MachineName == "Production.Server";
public static IKernel InitializeKernel()
{
var kernel = new StandardKernel();
// binding for production
kernel
.Bind<IBackend>()
.To<ProdBackend>()
.When(request => IsCurrentEnvironmentProduction());
// binding for test environment
kernel
.Bind<IBackend>()
.To<TestBackend>()
.When(request => !IsCurrentEnvironmentProduction());
return kernel;
}
public interface IBackend { }
public class ProdBackend : IBackend { }
public class TestBackend : IBackend { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment