当前位置: 首页>>代码示例>>C#>>正文


C# StandardKernel.Bind方法代码示例

本文整理汇总了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;
        }
开发者ID:Glipho,项目名称:oauth-provider-webapi-sample,代码行数:13,代码来源:NinjectWebCommon.cs

示例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);
        }
开发者ID:calebjenkins,项目名称:mvcturbine,代码行数:9,代码来源:NinjectResolutionTests.cs

示例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;
 }
开发者ID:hach-que,项目名称:Protoinject,代码行数:10,代码来源:EntityFactoryTests.cs

示例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);
            }
        }
开发者ID:Qiredev,项目名称:ninject,代码行数:12,代码来源:DefaultParameterTests.cs

示例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();
            }
        }
开发者ID:azcoov,项目名称:ninject,代码行数:13,代码来源:ConstructorSelectionTests.cs

示例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 );
            }
        }
开发者ID:brunomlopes,项目名称:ninject,代码行数:13,代码来源:ConstructorSelectionTests.cs

示例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;
        }
开发者ID:johnpro,项目名称:MediatR,代码行数:14,代码来源:Program.cs

示例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 );
            }
        }
开发者ID:brunomlopes,项目名称:ninject,代码行数:15,代码来源:ConstructorSelectionTests.cs

示例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();
        }
开发者ID:Haacked,项目名称:ninject,代码行数:28,代码来源:ActivationStrategyTests.cs

示例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);
            }
        }
开发者ID:Qiredev,项目名称:ninject,代码行数:17,代码来源:DefaultParameterTests.cs

示例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>();
            }
        }
开发者ID:brunomlopes,项目名称:ninject,代码行数:17,代码来源:ConstructorSelectionTests.cs

示例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;
        }
开发者ID:BredStik,项目名称:MediatR,代码行数:18,代码来源:Program.cs

示例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);
            }
        }
开发者ID:Qiredev,项目名称:ninject,代码行数:11,代码来源:DefaultParameterTests.cs

示例14: InstancesCanBeCreated

        public void InstancesCanBeCreated()
        {
            using (var kernel = new StandardKernel())
            {
                kernel.Bind<IConfig>().ToProvider<ConfigProvider>();

                var instance = kernel.Get<IConfig>();

                instance.Should().NotBeNull();
            }
        }
开发者ID:BenBtg,项目名称:ninject,代码行数:11,代码来源:ProviderTests.cs

示例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);
            }
        }
开发者ID:azcoov,项目名称:ninject,代码行数:11,代码来源:DefaultParameterTests.cs


注:本文中的StandardKernel.Bind方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。