Spring.net control Inversion (IoC) and environment configuration

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSpring.context;usingSpring.Context.Support;usingcommon.logging;namespacetest2{/// <summary>    ///before learning to spring.net this control inversion (IOC) and aspect-oriented (AOP) container framework, let's take a look at what a control reversal (IOC) is. ///control inversion (inversion of the control, English abbreviation for IOC), also called Dependency injection (Dependency injection). ///I personally think that the control reversal means that the dependent objects (control) change, the original class itself to manage the dependent objects into the IOC framework to manage these objects,///makes it possible to rely on the control of the detached class itself to achieve loose coupling. /// </summary>    classProgram {Static voidMain (string[] args) {            //Common ImplementationIpersondao DAO =NewPersondao (); Dao.            Save (); Console.WriteLine ("I'm the General method ."); //Program must know the Ipersondao interface and the Persondao class. In order not to expose specific implementations,//I can use the abstract Factory in the design pattern to solve the problem. Ipersondao daofactory =Dataaccess.createpersondao ();            Daofactory.save (); Console.WriteLine ("I'm the Factory method .");  Iocmethod (); //IOC Method "Console.ReadLine (); }        /// <summary>        ///this solves the problem of program-to-persondao coupling to a certain extent, but actually does not solve the coupling completely,///just put the coupling in the XML file, through a container when needed to form this dependency,///that is, inject the required interface implementation into the class that needs it. I personally think that the IOC model can be seen as a distillation of the factory model,///The IOC can be seen as a large factory, except that the objects to be generated in this large factory are defined in the XML file. /// </summary>        Private Static voidIocmethod () {Iapplicationcontext CTX=Contextregistry.getcontext (); Ipersondao DAO= CTX. GetObject ("Persondao") asIpersondao; if(DAO! =NULL) {dao.                Save (); Console.WriteLine ("I'm the IOC method ."); }        }    }     Public InterfaceIpersondao {voidSave (); }     Public classPersondao:ipersondao { Public voidSave () {Console.WriteLine ("Save Person"); }    }     Public Static classDataAccess { Public StaticIpersondao Createpersondao () {return NewPersondao (); }    }}
<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <configsections>    <!--with the following spring.net node configuration is a one by one correspondence relationship -    <sectiongroupname= "Spring">      < Sectionname= "Context"type= "Spring.Context.Support.ContextHandler, Spring.core"/>      < Sectionname= "Objects"type= "Spring.Context.Support.DefaultSectionHandler, Spring.core" />    </sectiongroup>  </configsections>  <Startup>    <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.0 "/>  </Startup>  <Spring>    <Context>      <ResourceURI= "Config://spring/objects"/>    </Context>    <Objectsxmlns= "Http://www.springframework.net">      <!--here, put all the nodes inside the container. -      <Description>An example that demonstrates the simple IoC features.</Description>      <!--name must be unique, the full name of the Type= class, where the assembly -      <Objectname= "Persondao"type= "Test2.persondao, Test2">      </Object>    </Objects>  </Spring></Configuration>

Spring.net control Inversion (IoC) and environment configuration

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.