Ninject Usage Introduction

Source: Internet
Author: User

    #regionThe second type of notation/// <summary>    ///using (Ikernel tkernel=new standardkernel (New Peokernelserver ()))/// {    ///Isay tsay= tkernel.get<ISay>(); ///ipeople tpeople = new Peoenglish (Tsay); ///Tpeople.sayhello (); /// }    /// </summary>     Public classPeokernelserver:ninjectmodule { Public Override voidLoad () {Bind<ISay> (). To<chinasay> ();//The local usage is similar to that of the first        }    }    #endregion    #regionThe first form of a notation/// <summary>    ///Isay Psay = PeoKernel.PeoKernelSigle.KernelSingle.Get<ISay>(); ///ipeople Peochina = new Peochina (Psay); ///peochina.name = "Chen"; ///Peochina.sayhello (); /// </summary>     Public classPeokernel {protected StaticPeokernel _peokernel =NewPeokernel ();  Public StaticPeokernel Peokernelsigle {Get            {                return_peokernel; }        }        protectedNinject.ikernel _kernel =NULL;  PublicNinject.ikernel Kernelsingle {Get            {                return_kernel; }        }        protectedPeokernel () {_kernel=NewStandardkernel ();        Bindclass (); }        protected voidBindclass () {#regionSimple binding//_kernel.bind<isay> (). To<chinasay> ();//1, general binding, must have no parameter constructor            #regionBinding method//Øto: A concrete implementation that binds to an interface. //øtoconstant: Binds to a constant value. //Øtomethod: Bind to method. //Øtoprovider: Binds to a custom object creation provider. //øtoself: self-binding            #endregion            //_kernel.bind<isay> (). To<englishsay> (). Withconstructorargument (true);//2. Constructor, parameter binding, can have no parameterless constructor            #regionParameter binding//øwithconstructorargument: Specifies the parameters associated with the constructor, as well as the overloads of the callback method. //Øwithmetadata: Specifies the relevant metadata information. //Øwithparameter: Specifies the relevant custom parameters. This method also implements the constructor parameter value designation, similar to withconstructorargument, such as:bind<imessage> (). To<messagedb> (). Withconstructorargument ("msg", 1); can also be written as:bind<imessage> (). To<messagedb> ().            Withparameter (New Constructorargument ("MSG", 1)); //Øwithpropertyvalue: Specifies the associated property value and the overload of the callback method.             #endregion            //_kernel.bind<isay> (). To<englishsay> (). Withpropertyvalue ("Isengorusalangle", false);//3. Attribute binding, there must be no parameter constructor. Can also be written as:bind<imessage> (). To<messagedb> ().            Withparameter (New Constructorargument ("MSG", 1)); //_kernel.bind<peochina> (). Toself ();//4. Self-binding. After self-binding, Isay does not need to be passed. Ipeople Peochina = peokernel.peokernelsigle.kernelsingle.get<peochina> ();            #endregion            #regionAttribute Injection//_kernel.bind<isay> ().            To<anisay> (); //_kernel.bind<anidog> (). Toself ();//in Anidog, the Isay property must have the [Ninject.inject] attribute added            #endregion            #regionConditional binding//designate Chinasay as the Isay in Peochina_kernel.bind<isay> (). To<chinasay> (). Wheninjectedinto<peochina> ();//1, general binding, must have no parameter constructor//specify Englishsay as the Isay in Peoenglish. Englishsay Pass parameter True at the same time_kernel.bind<isay> (). To<englishsay> (). Wheninjectedinto<peoenglish> (). Withconstructorargument (true);//2. Constructor, parameter binding, can have no parameterless constructor_kernel.bind<peochina>().            Toself (); _kernel.bind<PeoEnglish>().            Toself (); #endregion            #regionThread invocation_kernel.bind<ISay> (). To<chinasay> (). Inthreadscope ();//can be called by a thread            #regionThe call specifies that the object is in Inthreadscope, creating 2 threads in the code used for impersonation, and eventually each thread creates an object.using(Ikernel Tker =NewStandardkernel (NewPeokernelserver ())) {Thread tT1=NewThread (NewThreadStart (() ={Isay Tpeosay= tker.get<isay>();                    Tpeosay.saylanager ();                })); Thread tT2=NewThread (NewThreadStart (() ={Isay Tpeosay= tker.get<isay>();                Tpeosay.saylanager ();                }));                Tt1.start ();            Tt2.start (); }            #endregion            #endregion        }    }    #endregion    #regionPseudo code Public InterfaceIsay {stringSaylanager (); }     Public classChinasay:isay { PublicChinasay () {} Public stringSaylanager () {return "Chinese"; }    }     Public classEnglishsay:isay {Private BOOL_isengorusalangle =false;  Public BOOLIsengorusalangle {Get{return_isengorusalangle;} Set{_isengorusalangle =value;} }         PublicEnglishsay () {} PublicEnglishsay (BOOLisengorusalangle) {_isengorusalangle=Isengorusalangle; }         Public stringSaylanager () {stringStrenglan ="Englan"; if(_isengorusalangle) {Strenglan="Usalan"; }            returnStrenglan; }    }     Public InterfaceIpeople {stringName {Get;Set; } voidSayHello (); }     Public Abstract classPeoplebase:ipeople {protected string_name ="";  Public stringName {Get            {                return_name; }            Set{_name=value; }        }         Public Virtual voidSayHello () {Throw Newnotimplementedexception (); }    }     Public classPeochina:peoplebase { PublicPeochina () {}PrivateIsay _say;  PublicPeochina (Isay psay) {_say=Psay; }         Public Override voidSayHello () {MessageBox.Show (string. Format ("I am Chinese, my name is: {0}, I said: {1}", _name, _say.saylanager ())); }    }     Public classPeoenglish:peoplebase {PrivateIsay _say;  Publicpeoenglish () {} Publicpeoenglish (Isay psay) {_say=Psay; }         Public Override voidSayHello () {MessageBox.Show (string. Format ("I am english,my name is: {0},i say:{1}", _name, _say.saylanager ())); }    }     Public classAnisay:isay { PublicAnisay () {} Public stringSaylanager () {return "Wang"; }    }     Public classAnidog {[Ninject.inject]//Attribute Injection         PublicIsay Jiao {Get;Set; }  Public voidGoujiao () {MessageBox.Show (jiao.        Saylanager ()); }    }    #endregion

Ninject Usage Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.