Example #1
0
        private void mjButton17_Click(object sender, EventArgs e)
        {
            // ShoppingCart shoppingCart = new ShoppingCart(new CommoditySumValuation());
            ShoppingCart shoppingCart = new ShoppingCart(new CommoditySumValuation(new DisCount()));

            MJMessageBox.Show(this, shoppingCart.CommodityTotalPrice().ToString(), "MetroMessagebox", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #2
0
        private void mjButton20_Click(object sender, EventArgs e)
        {
            IKernel kernel  = new StandardKernel(new MyModule());
            ILogger logger  = kernel.Get <ILogger>();    //获取的是ILogger
            ILogger loggers = kernel.Get <FileLogger>(); //获取的是FileLogger

            MJMessageBox.Show(this, "温馨提示", loggers.Write("测试一下哟1"), MessageBoxButtons.OK, MessageBoxIcon.Information);
            MJMessageBox.Show(this, "温馨提示", logger.Write("测试一下哟2"), MessageBoxButtons.OK, MessageBoxIcon.Information);

            //用到的方法
            //(1) Bind<T1>().To<T2>()
            //其实就是接口IKernel的方法,把某个类绑定到某个接口,T1代表的就是接口或者抽象类,
            //而T2代表的就是其实现类
            //IKernel ninjectKernel = new StandardKernel();
            // ninjectKernel.Bind<ILogger>().To<FileLogger>();
            //(2) Get<ISomeInterface>()
            //其实就是得到某个接口的实例,例如下面的例子就是得到ILogger的实例FileLogger:
            //ILogger myLogger = ninjectKernel.Get<ILogger>();
            //(3) Bind<T1>().To<T2>(). WithPropertyValue("SomeProprity", value);
            //其实就是在绑定接口的实例时,同时给实例NinjectTester的属性赋值,例如:
            //ninjectKernel.Bind<ITester>().To<NinjectTester>().WithPropertyValue("_Message", "这是一个属性值注入");
            // (5)Bind<T1 >() .ToConstant ()
            //这个方法的意思是绑定到某个已经存在的常量,例如
            //   StudentRepository sr = new StudentRepository();
            // ninjectKernel.Bind<IStudentRepository>().ToConstant(sr);
            //(6) Bind<T1 >() .ToSelf()
            // 这个方法意思是绑定到自身,但是这个绑定的对象只能是具体类,不能是抽象类;
            //为什么要自身绑定呢?其实也就是为了能够利用Ninject解析对象本身而已。例如:
            // ninjectKernel.Bind<StudentRepository>().ToSelf();
            //StudentRepository sr = ninjectKernel.Get<StudentRepository>();
            //(7)Bind<T1>().To<T2>().WhenInjectedInto<instance>()
            //这个方法是条件绑定,就是说只有当注入的对象是某个对象的实例时才会将绑定的接口进行实例化
            // ninjectKernel.Bind<IValueCalculater>().To<IterativeValueCalculatgor>().WhenInjectedInto<LimitShoppingCart>();
        }
Example #3
0
        private void mjButton22_Click(object sender, EventArgs e)
        {
            IKernel  kernal = new StandardKernel(new WarriorModule());
            Samurai2 s      = new Samurai2();

            s._weapon = kernal.Get <IWeapon>(); // 需将Samurai类中字段_weapon修饰符改为public才可以访问
            MJMessageBox.Show(this, "温馨提示", s.Attack("属性注入"), MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #4
0
        private void mjButton21_Click(object sender, EventArgs e)
        {
            IKernel kernal = new StandardKernel(new WarriorModule());
            Samurai s      = new Samurai();

            s.Arm(kernal.Get <IWeapon>()); // 方法注入
            MJMessageBox.Show(this, "温馨提示", s.Attack("方法注入"), MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #5
0
        private void mjButton26_Click(object sender, EventArgs e)
        {
            string str_TaskType = "Pipe";
            //var dd = Enum.GetNames(typeof(TaskType.Pipe));
            var getType = (TaskType)Enum.Parse(typeof(TaskType), str_TaskType);
            var values  = getType.GetHashCode().ToString();

            MJMessageBox.Show(this, "温馨提示", values, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #6
0
        private void mjButton23_Click(object sender, EventArgs e)
        {
            person pro = new person();//实例化类对象


            ITeacher iteach = pro;      //使用派生类对象实例化接口ITeacher

            iteach.Name = "橙子";
            iteach.Sex  = "男";
            MJMessageBox.Show(this, "温馨提示", iteach.teach().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);

            IStudent istu = pro;        //使用派生类对象实例化接口IStudent

            istu.Name = "C#";
            MJMessageBox.Show(this, "温馨提示", istu.study().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #7
0
        private void mjButton16_Click(object sender, EventArgs e)
        {
            //#region IoC框架功能(一)
            //IKernel kernel = new StandardKernel();
            //kernel.Bind<IValuation>().To<CommoditySumValuation>();
            //IValuation valuation = kernel.Get<IValuation>();
            //#endregion
            IKernel kernel = new StandardKernel();//(二)

            kernel.Bind <IValuation>().To <CommoditySumValuation>();
            kernel.Bind <IValuationDisCount>().To <DisCount>();
            IValuation valuation = kernel.Get <IValuation>();

            ShoppingCart shoppingCart = new ShoppingCart(valuation);

            MJMessageBox.Show(this, shoppingCart.CommodityTotalPrice().ToString(), "MetroMessagebox", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #8
0
        private void mjButton18_Click(object sender, EventArgs e)
        {
            //   public class Module1 {
            //      public override  void Load() { ... }
            //}

            //public class Module2 {
            //      public override  void Load() { ... }
            //}

            //class Program {
            //      public static void Main()
            //      {
            //            IKernel kernel = new StandardKernel(new Module1(), new Module2(), ...);
            //            ...
            //      }
            //}  多个案例
            IKernel  kernal = new StandardKernel(new WarriorModule());
            Samurai1 s1     = new Samurai1(kernal.Get <IWeapon>()); // 构造函数注入
            Samurai1 s      = kernal.Get <Samurai1>();

            s.Attack("enemy");
            MJMessageBox.Show(this, "温馨提示", s.Attack("构造函数注入"), MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #9
0
        private void mjButton25_Click(object sender, EventArgs e)
        {
            var iteach = Description(TaskType.Pipe);

            MJMessageBox.Show(this, "温馨提示", iteach, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #10
0
 private void mjButton14_Click(object sender, EventArgs e)
 {
     MJMessageBox.Show(this, "This is a sample MetroMessagebox `Abort`, `Retry` and `Ignore` button.  With Error style.", "MetroMessagebox", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
 }
Example #11
0
 private void mjButton13_Click(object sender, EventArgs e)
 {
     MJMessageBox.Show(this, "This is a sample MetroMessagebox `Retry` and `Cancel` button.  With warning style.", "MetroMessagebox", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
 }
Example #12
0
 private void mjButton12_Click(object sender, EventArgs e)
 {
     MJMessageBox.Show(this, "This is a sample `default` MetroMessagebox ", "MetroMessagebox");
 }
Example #13
0
 private void mjButton11_Click(object sender, EventArgs e)
 {
     MJMessageBox.Show(this, "This is a sample MetroMessagebox `Yes`, `No` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
 }
Example #14
0
 private void mjButton9_Click(object sender, EventArgs e)
 {
     MJMessageBox.Show(this, "This is a sample MetroMessagebox `OK` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
 }