Share an example of an IOC introductory tutorial

Source: Internet
Author: User
Spring.net includes control inversion (IOC) and aspect-oriented (AOP), this article mainly introduces the IoC aspects of the introduction.

First, build an MVC project name called Springdemo, then download spring with NuGet (I'm using spring.net NHibernate 4 support)

Second, class design, under the Models folder to establish a class, the main Iuserinfo,userinfo,order three class code as follows:

Public interface Iuserinfo    {string Showmeg ();    }
public class Userinfo:iuserinfo    {public string UserName {get; set;} Public Order by {get; set;} public string Showmeg ()        {return name: "+ UserName +" Order Number: "+ Orderby.orderno;        }    }
public class Order    {public string OrderNo {get; set;}    }

Third, enter the key step: Modify the configuration file. Modify the following directly in the Web. config:

<sectiongroup name= "Spring" > <!--parsing Spring block objects--<section name= "context" type= " Spring.context.support.contexthandler,spring.core "/> <!--configuration parsing the container collection of Spring storage objects--<section name=" Objects "type=" Spring.context.support.defaultsectionhandler,spring.core "/></sectiongroup> </ configsections><!--****************** Spring configuration start ******************--> <spring><context> < !--container configuration, configure the current container object to be placed in the top position: currently in the current configuration file--<resource uri= "config://spring/objects"/><!--current--></ Context><objects xmlns= "Http://www.springframework.net" > <!--here to store all the container nodes--<description>an Example that demonstrates the simple Ioc features</description> <!--name must be uniquely named, typically the type name, the full name of the Type= class, the assembly, The goal is to let the container easily reflect objects--<object name= "UserInfo" type= "Springdemo.models.userinfo,springdemo" ><property Name= "UserName" value= "Lao Wang"/><!--ref points to Zodiac injection--><property name= "Order" ref=/>Gt <object name= "Order" type= "Springdemo.models.order,springdemo" ><property name= "OrderNo" value= "20170808"/ > </object></objects> </spring> <!--****************** Spring configuration End ******************-->

Four, the code test, the new controller, the code is as follows:

Public ActionResult Index ()        {            Iapplicationcontext ctx = Contextregistry.getcontext ();            Iuserinfo lister = (iuserinfo) ctx. GetObject ("UserInfo");            Viewbag.msg = Lister. Showmeg (); return View ();        }

V. The foreground adds the display @ViewBag. MSG operation results are as follows:

Six, before the finished, but if you want to separate the configuration file out how to do it, in order to achieve the test effect of a new class Newuserinfo code as follows:

public class Newuserinfo:iuserinfo    {public newuserinfo (string name, order order)        {this. UserName = Name;this. by order;        } public string UserName {get; set;} Public Order by {get; set;} public string Showmeg ()        {return name: "+ UserName +" Order Number: "+ Orderby.orderno;        }    }

Create the Objects.xml property setting under the New Folder config to create the contents of the directory as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><objects xmlns= "Http://www.springframework.net" >  <!-- This holds all the container nodes--  <description>an example that demonstrates the simple Ioc features</description>    <!--constructor injection--  <object name= "Newuserinfo" type= "Springdemo.models.newuserinfo,springdemo" >< Constructor-arg index= "0" value= "Jacky"/><constructor-arg index= "1" ref= "Order"/>  </object>  <!--complex Dependency injection--  <object name= "Order" type= "Springdemo.models.order,springdemo" ><property name= " OrderNo "value=" 20170909 "/>  </object>   </objects>

Viii. modify Web. config to specify Objects.xml as the parsing dependency

<resource uri= "~/config/objects.xml"/><!--specified document--

Nine, under the home controller to establish the action Newuserinfo code as follows:

Public ActionResult Newuserinfo ()        {            Iapplicationcontext ctx = Contextregistry.getcontext ();            Iuserinfo lister = (iuserinfo) ctx. GetObject ("Newuserinfo");            Viewbag.msg = Lister. Showmeg (); return View ();        }

Ten, the foreground adds the display @ViewBag. MSG operation results are as follows:

Summary: The IOC has brought us a lot of convenience, and when we find it difficult to use simple factories, we can use IOC instead, and most of them support AOP.

Good luck, June.

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.