Spring.net Introduction and application in MVC

Source: Internet
Author: User

Spring.net Two core content:

    • IOC (Control inversion)

The traditional thinking mode of face object is object A depends on object B, the instantiation and invocation of object B occur in object A, and once the object B changes, object A also changes, which makes the travel between programs tight coupling degree

The IOC is a programming idea whose idea is to decouple with the help of a third party. The IOC core is an IOC container, and all control over all objects is given to the IOC container, and the IOC container is responsible for creating the object. The schema is created by the original object--using objects to the passive receive IOC container--using the object, during which the control of the object is reversed, so called control inversion

    • DI (Dependency injection)

Di is a programmatic way to implement control inversion, which is simply to use the B object in a object, then it is necessary to have new B () in the A object, but suppose the B object changes and the incoming parameter changes when instantiated, it is necessary to modify the code of a.

The solution is very simple, that is, B as an object in, a object directly received this.b = B;

After the introduction of the principle to see how to use:

1. Download Spring.net package : http://www.springframework.net/download.html

2. unzip and copy the \spring.net-1.3.1\bin\net\4.0\release directory

Common.Logging.dllSpring.Core.dllSpring.Web.dllSpring.Web.Extensions.dllSpring.Web.Mvc.dll

Extension files to the project Lib directory and add references to the Web project

3. add the Config directory under the project to create the Controllers.xml

Registering the Controller class in Controllers.xml

<?xml version="1.0"encoding="Utf-8"? ><objects xmlns="http://www.springframework.net"> <ObjectType="MyProject.Controllers.UserInfoController, MyProject"singleton="false"> <property name="Userinfoservice" ref="Userinfoservice"/> </Object> <!--intentionally DoNot register the AccountController with the container; demonstrates the underlyingdefaultController factory'll properly (attempt to!) Resolve all controllers not registered with Spring.netusingItsdefaultController Resolution Behavoir--> <!--<ObjectType="Spring.MvcQuickStart.Controllers.AccountController, Spring.mvcquickstart"singleton="false"/>--></objects>

The object node is used to register the controller,

Under the Type property, declare the namespace + class name, assembly,

The property attribute is used to declare properties in the Controller class, and if it is a reference type, it is required to point to its service.xml under the Name property of the Registered object class

4. create a service.xml in the Config directory

Register the various classes in the BLL layer under this XML file

 <?xml version= " 1.0  "  encoding= " utf-8   "? ><objects xmlns="  http://www.springframework.net  "  > <object  type="   MyProject.BLL.UserInfoService, MYPROJECT.BLL   " Singleton="  false   " name=< Span style= "COLOR: #800000" > " userinfoservice   " > </object  ></objects> 

5. register the following information under the Web. config file

Add under the <configSections><configSections> node

    <sectiongroup name="spring">      <!--spring. NET configuration--      <section name="context" type="  Spring.Context.Support.MvcContextHandler, SPRING.WEB.MVC"/>    </sectionGroup>

Add under the root node <configuration></configuration> node

  <spring>    <!--spring. NET configuration    -<context>    <!--modified to the appropriate directory--      <resource uri="file://~/Config/ Controllers.xml"/>      <resource uri="file://~/Config/Service.xml" />    </context>  </spring>

6. Finally, don't forget to modify the Golbal.asax entry file

     Public class // System.Web.HttpApplication     {        protectedvoid  Application_Start ()        {            Arearegistration.registerallareas ();            Webapiconfig.register (globalconfiguration.configuration);            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (bundletable.bundles);        }    }

The decoupling of the business layer and the logical layer has been completed.

You only need to declare the properties under Controller controllers and automatically pass in the appropriate object when the program is run

Private Get set;}

Spring.net Introduction and application in MVC

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.