Using NInject do DI(2)

Last Article introduced brief about NInject usage in c# .


This Article will continue introduce how to use NInject in C# handle Dependency chain .

Test Dependency Chain :



1.Add IDiscount Interface :

  interface IDiscount<T>
    {
        T WithDiscount(T value);
    }



2.Implementation :

class DiscountHalf<T> : IDiscount<T>
    {
        public T WithDiscount(T value)
        {
            return (T)((dynamic)value * 0.5);
        }
    }


  class NoDiscount<T>:IDiscount<T>
    {
        public T WithDiscount(T value)
        {
            return value;
        }
    }





3. Modify Calculators :


class CalculatorAdd<T> : ICalculate<T>
    {
        private readonly IDiscount<T> _discount; 
        public CalculatorAdd(IDiscount<T> discount)
        {
            _discount = discount;
        } 
        public T Operate(T a, T b)
        {
            return _discount.WithDiscount(((dynamic)a + (dynamic)b));
        }
    }


  class CalculatorDiv<T> : ICalculate<T>
    {
        private readonly IDiscount<T> _discount; 
        public CalculatorDiv(IDiscount<T> discount)
        {
            _discount = discount;
        }
        public T Operate(T a, T b)
        {
            return _discount.WithDiscount((dynamic)a / (dynamic)b);
        }
    }


  class CalculatorMul<T> : ICalculate<T>
    {
        private readonly IDiscount<T> _discount;
        public CalculatorMul(IDiscount<T> discount)
        {
            _discount = discount;
        }
        public T Operate(T a, T b)
        {
            return _discount.WithDiscount((dynamic)a * (dynamic)b);
        }
    }


  class CalculatorSub<T> : ICalculate<T>
    {
        private readonly IDiscount<T> _discount;
        public CalculatorSub(IDiscount<T> discount)
        {
            _discount = discount;
        }
        public T Operate(T a, T b)
        {
            return _discount.WithDiscount((dynamic)a - (dynamic)b);
        }
    }





4.Modify DI Modules(Bind IDiscount<T>):


 
class DIModule : Ninject.Modules.NinjectModule
    {
        public override void Load()
        {
            Bind<ICalculate<int>>().To<CalculatorAdd<int>>();
            Bind<ICalculate<double>>().To<CalculatorSub<double>>();


            Bind<IDiscount<int>>().To<Discount.DiscountHalf<int>>();
            Bind<IDiscount<double>>().To<Discount.DiscountHalf<double>>();


        }
    }






  class DIModule2:Ninject.Modules.NinjectModule
    {
        public override void Load()
        {
            Bind<ICalculate<int>>().To<CalculatorMul<int>>();
            Bind<ICalculate<double>>().To<CalculatorDiv<double>>();


            Bind<IDiscount<int>>().To<Discount.NoDiscount<int>>();
            Bind<IDiscount<double>>().To<Discount.NoDiscount<double>>();
        }
    }






5.Remain Program.cs Code :


class Program
    {
        private StandardKernel _diManager ;
        
        static void Main(string[] args)
        {
            var _diManager = new StandardKernel(new DIModule());
            var r1 = _diManager.Get<ICalculate<int>>().Operate(1, 4);
            Console.WriteLine(r1);//r1 = (1+4 )*0.5
            var r2 = _diManager.Get<ICalculate<double>>().Operate(4, 1);
            Console.WriteLine(r2);//r2=(4-1) * 0.5


            //switch module
            _diManager = new StandardKernel(new DIModule2());
            var r3 = _diManager.Get<ICalculate<int>>().Operate(2, 4);
            Console.WriteLine(r3);//r3 = 2*4
            var r4 = _diManager.Get<ICalculate<double>>().Operate(4, 2);
            Console.WriteLine(r4);//r4 = 4/2


            Console.ReadKey();
        }
    }




你可能感兴趣的:(Using NInject do DI(2))