zoukankan      html  css  js  c++  java
  • Ninject使用介绍

        #region 第二种写法
        /// <summary>
        ///  using(IKernel tKernel=new StandardKernel(new PeoKernelServer()))
        /// {
        ///     ISay tSay= tKernel.Get<ISay>();
        ///     IPeople tPeople = new PeoEnglish(tSay);
        ///     tPeople.sayHello();
        /// }
        /// </summary>
        public class PeoKernelServer : NinjectModule
        {
            public override void Load()
            {
                Bind<ISay>().To<ChinaSay>();//这个地方用法类似第一种
            }
        }
        #endregion
        #region 第一种写法
        /// <summary>
        /// ISay pSay = PeoKernel.PeoKernelSigle.KernelSingle.Get<ISay>();
        /// IPeople pEoChina = new PeoChina(pSay);
        /// pEoChina.Name = "陈国岭";
        /// pEoChina.sayHello();
        /// </summary>
        public class PeoKernel
        {
            protected static PeoKernel _PeoKernel = new PeoKernel();
            public static PeoKernel PeoKernelSigle
            {
                get
                {
                    return _PeoKernel;
                }
            }
            protected Ninject.IKernel _Kernel = null;
            public Ninject.IKernel KernelSingle
            {
                get
                {
                    return _Kernel;
                }
            }
            protected PeoKernel()
            {
                _Kernel = new StandardKernel();
                BindClass();
            }
            protected void BindClass()
            {
                #region  简单绑定
    
                //_Kernel.Bind<ISay>().To<ChinaSay>();//1、一般绑定,必须有无参数构造函数
                #region 绑定方法
                //Ø To:绑定到接口的具体实现。
                //Ø ToConstant:绑定到某个常量值。
                //Ø ToMethod:绑定到方法。
                //Ø ToProvider:绑定到某个自定义的对象创建提供程序。
                //Ø ToSelf:自绑定
                #endregion
                //_Kernel.Bind<ISay>().To<EnglishSay>().WithConstructorArgument(true);//2.构造函数中,参数绑定,可以没有无参数构造函数
                #region 参数绑定
                // Ø WithConstructorArgument:指定构造函数中相关的参数,还有回调方法的重载。
                //Ø WithMetadata:指定相关元数据信息。
                //Ø WithParameter:指定相关的自定义参数。这个方法也实现构造函数参数值指定,与WithConstructorArgument类似,如:Bind<IMessage>().To<MessageDB>().WithConstructorArgument("msg", 1);同样可以写成:Bind<IMessage>().To<MessageDB>().WithParameter(new ConstructorArgument("msg", 1));
                //Ø WithPropertyValue:指定相关的属性值,还有回调方法的重载。
                #endregion
                //_Kernel.Bind<ISay>().To<EnglishSay>().WithPropertyValue("IsEngOrUSALangle", false);//3.属性绑定,必须有无参构造函数。同样可以写成:Bind<IMessage>().To<MessageDB>().WithParameter(new ConstructorArgument("msg", 1));
                //_Kernel.Bind<PeoChina>().ToSelf();//4、自我绑定.自我绑定后,ISay无需传递。IPeople pEoChina = PeoKernel.PeoKernelSigle.KernelSingle.Get<PeoChina>();
    
                #endregion
                #region 属性注入
                //_Kernel.Bind<ISay>().To<AniSay>();
                //_Kernel.Bind<AniDog>().ToSelf();//在AniDog中,ISay属性必须添加[Ninject.Inject]特性
                #endregion
                #region 条件绑定
                //把ChinaSay指定为PeoChina中的ISay
                _Kernel.Bind<ISay>().To<ChinaSay>().WhenInjectedInto<PeoChina>();//1、一般绑定,必须有无参数构造函数
                //把EnglishSay指定为PeoEnglish中的ISay。同时EnglishSay传递参数true
                _Kernel.Bind<ISay>().To<EnglishSay>().WhenInjectedInto<PeoEnglish>().WithConstructorArgument(true);//2.构造函数中,参数绑定,可以没有无参数构造函数
                _Kernel.Bind<PeoChina>().ToSelf();
                _Kernel.Bind<PeoEnglish>().ToSelf();
                #endregion
                #region 线程调用
                _Kernel.Bind<ISay>().To<ChinaSay>().InThreadScope();//可以线程调用
                #region 调用 指定了对象在InThreadScope,在使用的代码中分别创建了2个线程来进行模拟,最终每个线程都是创建了一个对象。
                using (IKernel tKer = new StandardKernel(new PeoKernelServer()))
                {
                    Thread tT1 = new Thread(new ThreadStart(() =>
                        {
                            ISay tPeoSay = tKer.Get<ISay>();
                            tPeoSay.SayLanager();
                        }));
                    Thread tT2 = new Thread(new ThreadStart(() =>
                    {
                        ISay tPeoSay = tKer.Get<ISay>();
                        tPeoSay.SayLanager();
                    }));
                    tT1.Start();
                    tT2.Start();
                }
                #endregion
    
                #endregion
            }
        }
        #endregion
        #region 伪代码
        public interface ISay
        {
            string SayLanager();
        }
        public class ChinaSay : ISay
        {
            public ChinaSay()
            {
    
            }
            public string SayLanager()
            {
                return "汉语";
            }
        }
        public class EnglishSay : ISay
        {
            private bool _isEngOrUSALangle = false;
    
            public bool IsEngOrUSALangle
            {
                get { return _isEngOrUSALangle; }
                set { _isEngOrUSALangle = value; }
            }
            public EnglishSay()
            {
    
            }
            public EnglishSay(bool isEngOrUSALangle)
            {
                _isEngOrUSALangle = isEngOrUSALangle;
            }
            public string SayLanager()
            {
                string strEngLan = "EngLan";
                if (_isEngOrUSALangle)
                {
                    strEngLan = "USALan";
                }
                return strEngLan;
            }
        }
        public interface IPeople
        {
            string Name { get; set; }
            void sayHello();
        }
        public abstract class PeopleBase : IPeople
        {
            protected string _Name = "";
            public string Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    _Name = value;
                }
            }
    
            public virtual void sayHello()
            {
                throw new NotImplementedException();
            }
        }
        public class PeoChina : PeopleBase
        {
            public PeoChina()
            {
            }
            private ISay _Say;
            public PeoChina(ISay pSay)
            {
                _Say = pSay;
            }
            public override void sayHello()
            {
                MessageBox.Show(string.Format("我是中国人,我叫:{0},我说:{1}", _Name, _Say.SayLanager()));
            }
        }
        public class PeoEnglish : PeopleBase
        {
            private ISay _Say;
            public PeoEnglish()
            {
    
            }
            public PeoEnglish(ISay pSay)
            {
                _Say = pSay;
            }
            public override void sayHello()
            {
                MessageBox.Show(string.Format("I am English,my name is :{0},I Say:{1}", _Name, _Say.SayLanager()));
            }
        }
    
        public class AniSay : ISay
        {
            public AniSay()
            {
    
            }
            public string SayLanager()
            {
                return "wang";
            }
        }
        public class AniDog
        {
            [Ninject.Inject]//属性注入
            public ISay jiao { get; set; }
            public void gouJiao()
            {
                MessageBox.Show(jiao.SayLanager());
            }
        }
        #endregion
  • 相关阅读:
    cmd输入输出重定向
    【转载】标准输入输出、错误输出、重定向标准输出
    cmd 重定向
    Windows下cmd标准输入输出重定向
    Windows 命令行输入输出重定向问题
    MATLAB数值计算与符号运算
    选择排序法 冒泡排序法 本质上是对内存进行整理
    学习笔记:Monkey runner(一)
    Monkey test
    fiddler:快速标识接口
  • 原文地址:https://www.cnblogs.com/cglNet/p/4228334.html
Copyright © 2011-2022 走看看