本文整理汇总了C#中StandardKernel.Bind方法的典型用法代码示例。如果您正苦于以下问题:C# StandardKernel.Bind方法的具体用法?C# StandardKernel.Bind怎么用?C# StandardKernel.Bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StandardKernel
的用法示例。
在下文中一共展示了StandardKernel.Bind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateKernel
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
示例2: CreateServiceLocator
protected override IServiceLocator CreateServiceLocator() {
var kernel = new StandardKernel();
var simpleType = typeof(SimpleLogger);
kernel.Bind<ILogger>().To<SimpleLogger>().Named(simpleType.FullName);
var loggerType = typeof(ComplexLogger);
kernel.Bind<ILogger>().To<ComplexLogger>().Named(loggerType.FullName);
return new NinjectServiceLocator(kernel);
}
示例3: CreateKernel
private IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<IEntityFactory>().ToFactory();
kernel.Bind<IPlayer>().To<Player>();
kernel.Bind<INetworkingPlayer>().To<NetworkingPlayer>();
kernel.Bind<IMovement>().To<DefaultMovement>();
kernel.Bind<IInput>().To<DefaultInput>();
return kernel;
}
示例4: SpecificValueShouldBeUsedWhenMapped
public void SpecificValueShouldBeUsedWhenMapped()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<Shield>().ToSelf();
kernel.Bind<ShieldColor>().ToConstant(ShieldColor.Blue);
var shield = kernel.Get<Shield>();
shield.ShouldNotBeNull();
shield.Color.ShouldBe(ShieldColor.Blue);
}
}
示例5: FirstAvailableWithBindingAvailableIsUsed
public void FirstAvailableWithBindingAvailableIsUsed()
{
using ( IKernel kernel = new StandardKernel() )
{
kernel.Bind<Barracks>().ToSelf();
kernel.Bind<IWeapon>().To<Sword>();
var barracks = kernel.Get<Barracks>();
barracks.ShouldNotBeNull();
barracks.Warrior.ShouldBeNull();
barracks.Weapon.ShouldNotBeNull();
}
}
示例6: FirstAvailableWithBindingAvailableIsUsed
public void FirstAvailableWithBindingAvailableIsUsed()
{
using ( IKernel kernel = new StandardKernel() )
{
kernel.Bind<Barracks>().ToSelf();
kernel.Bind<IWeapon>().To<Sword>();
var barracks = kernel.Get<Barracks>();
Assert.NotNull( barracks );
Assert.Null( barracks.Warrior );
Assert.NotNull( barracks.Weapon );
}
}
示例7: BuildMediator
private static IMediator BuildMediator()
{
var kernel = new StandardKernel();
kernel.Components.Add<IBindingResolver, ContravariantBindingResolver>();
kernel.Bind(scan => scan.FromAssemblyContaining<IMediator>().SelectAllClasses().BindDefaultInterface());
kernel.Bind(scan => scan.FromAssemblyContaining<Ping>().SelectAllClasses().BindAllInterfaces());
kernel.Bind<TextWriter>().ToConstant(Console.Out);
kernel.Bind<SingleInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.Get(t));
kernel.Bind<MultiInstanceFactory>().ToMethod(ctx => t => ctx.Kernel.GetAll(t));
var mediator = kernel.Get<IMediator>();
return mediator;
}
示例8: CtorWithMostDependenciesIsUsedWhenBindingsAreAvailable
public void CtorWithMostDependenciesIsUsedWhenBindingsAreAvailable()
{
using ( IKernel kernel = new StandardKernel() )
{
kernel.Bind<Barracks>().ToSelf();
kernel.Bind<IWeapon>().To<Sword>();
kernel.Bind<IWarrior>().To<Samurai>();
var barracks = kernel.Get<Barracks>();
Assert.NotNull( barracks );
Assert.NotNull( barracks.Warrior );
Assert.NotNull( barracks.Warrior.Weapon );
Assert.NotNull( barracks.Weapon );
}
}
示例9: InstanceIsDeactivatedWhenItLeavesScope
public void InstanceIsDeactivatedWhenItLeavesScope()
{
Barracks barracks;
using ( var kernel = new StandardKernel() )
{
kernel.Bind<Barracks>()
.ToSelf()
.InSingletonScope()
.OnActivation(instance =>
{
instance.Warrior = new FootSoldier();
instance.Weapon = new Shuriken();
})
.OnDeactivation(instance =>
{
instance.Warrior = null;
instance.Weapon = null;
});
barracks = kernel.Get<Barracks>();
barracks.Warrior.ShouldNotBeNull();
barracks.Warrior.ShouldBeInstanceOf<FootSoldier>();
barracks.Weapon.ShouldNotBeNull();
barracks.Weapon.ShouldBeInstanceOf<Shuriken>();
}
barracks.Warrior.ShouldBeNull();
barracks.Weapon.ShouldBeNull();
}
示例10: DefaultValuesShouldNotInflunceInjectionsToOtherTypes
public void DefaultValuesShouldNotInflunceInjectionsToOtherTypes()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<Shield>().ToSelf();
kernel.Bind<KiteShield>().ToSelf();
var shield1 = kernel.Get<Shield>();
var shield2 = kernel.Get<KiteShield>();
shield1.ShouldNotBeNull();
shield1.Color.ShouldBe(ShieldColor.Red);
shield2.ShouldNotBeNull();
shield2.Color.ShouldBe(ShieldColor.Orange);
}
}
示例11: CreationWillFailIfAllDepenciesAreMissingAndInjectAttributeIsApplied
public void CreationWillFailIfAllDepenciesAreMissingAndInjectAttributeIsApplied()
{
using ( IKernel kernel = new StandardKernel() )
{
kernel.Bind<NinjaBarracks>().ToSelf();
Assert.Throws<ActivationException>( () => kernel.Get<NinjaBarracks>() );
kernel.Bind<IWeapon>().To<Sword>();
Assert.Throws<ActivationException>( () => kernel.Get<NinjaBarracks>() );
kernel.Unbind<IWeapon>();
kernel.Bind<IWarrior>().To<Samurai>();
Assert.Throws<ActivationException>( () => kernel.Get<NinjaBarracks>() );
kernel.Unbind<IWarrior>();
}
}
示例12: BuildMediator
private static IMediator BuildMediator()
{
var kernel = new StandardKernel();
kernel.Components.Add<IBindingResolver, ContravariantBindingResolver>();
kernel.Bind(scan => scan.FromAssemblyContaining<IMediator>().SelectAllClasses().BindDefaultInterface());
kernel.Bind(scan => scan.FromAssemblyContaining<Ping>().SelectAllClasses().BindAllInterfaces());
kernel.Bind<TextWriter>().ToConstant(Console.Out);
var serviceLocator = new NinjectServiceLocator(kernel);
var serviceLocatorProvider = new ServiceLocatorProvider(() => serviceLocator);
kernel.Bind<ServiceLocatorProvider>().ToConstant(serviceLocatorProvider);
var handlers = kernel.GetAll<INotificationHandler<Pinged>>();
var mediator = serviceLocator.GetInstance<IMediator>();
return mediator;
}
示例13: DefaultValueShouldBeUsedWhenNoneSupplied
public void DefaultValueShouldBeUsedWhenNoneSupplied()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<Shield>().ToSelf();
var shield = kernel.Get<Shield>();
shield.ShouldNotBeNull();
shield.Color.ShouldBe(ShieldColor.Red);
}
}
示例14: InstancesCanBeCreated
public void InstancesCanBeCreated()
{
using (var kernel = new StandardKernel())
{
kernel.Bind<IConfig>().ToProvider<ConfigProvider>();
var instance = kernel.Get<IConfig>();
instance.Should().NotBeNull();
}
}
示例15: SpecificValueShouldBeUsedWhenSupplied
public void SpecificValueShouldBeUsedWhenSupplied()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<Shield>().ToSelf().WithConstructorArgument("color", ShieldColor.Orange);
var shield = kernel.Get<Shield>();
shield.ShouldNotBeNull();
shield.Color.ShouldBe(ShieldColor.Orange);
}
}