예제 #1
0
        static void Main(string[] args)
        {
            IKernel ninjectKernel = new StandardKernel();
            ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
            ninjectKernel.Bind<IDiscountHelper>()
                .To<DefaultDiscountHelper>()
                .WithConstructorArgument("sizeParam", 50M);
            //.WithPropertyValue("DiscountSize", 20M);
            IValueCalculator calcImpl = ninjectKernel.Get<IValueCalculator>();

            ShoppingCart cart = new ShoppingCart(calcImpl);
            Console.WriteLine("Total:{0:c} ", cart.CalculateStockValue());

            //Ninject SelfBinding
            ShoppingCart selfBindingCart = ninjectKernel.Get<ShoppingCart>();
            Console.WriteLine("Total:{0:c} ", selfBindingCart.CalculateStockValue());

            //Self binding with parameters
            //ninjectKernel.Bind<ShoppingCart>().ToSelf().WithPropertyValue("", "");

            ninjectKernel.Bind<ShoppingCart>().To<LimitShoppingCart>().WithPropertyValue("ItemLimit", 200M);
            ShoppingCart derivedTypeBindingCart = ninjectKernel.Get<ShoppingCart>();
            Console.WriteLine("Total:{0:c} ", derivedTypeBindingCart.CalculateStockValue());

            //Conditional Binding Methods
            ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
            ninjectKernel.Bind<IValueCalculator>()
            .To<IterativeValueCalculator>()
            .WhenInjectedInto<LimitShoppingCart>();

            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            //remark
            var ninjectKernel=new StandardKernel();
            ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();//绑定到LinValueCalculator的实例中
            //应为LinqValueCalculator的实例是依赖DefaultDiscountor的,因此要绑定到DefaultDiscountHelper的实例中
            ninjectKernel.Bind<IDiscountHelper>().To<DefaultDiscountHelper>().WithConstructorArgument("discountParam", 40M);

            var calcImpl = ninjectKernel.Get<IValueCalculator>();
            var cart=new ShoppingCart(calcImpl);
            Console.WriteLine("Total :{0:c}",cart.CalculateStockValue());
        }