Home  >  Article  >  Backend Development  >  Share an IoC introductory tutorial example

Share an IoC introductory tutorial example

零下一度
零下一度Original
2017-06-24 09:54:032060browse

Spring.Net includes Inversion of Control (IoC) and Aspect Orientation (AOP). This article mainly talks about getting started with IoC.

1. First create an MVC project named SpringDemo, and then use NuGet to download spring (I use Spring.Net NHibernate 4 support)

2. Class design, under the Models folder Create classes, the main three class codes of IUserInfo, UserInfo and Order are as follows:

public interface IUserInfo
    {string ShowMeg();
    }
public class UserInfo : IUserInfo
    {public string UserName { get; set; }public Order OrderBy { get; set; }public string ShowMeg()
        {return "姓名:" + UserName + "订单号:" + OrderBy.OrderNo;
        }
    }
public class Order
    {public string OrderNo { get; set; }
    }

3. Enter the critical step: modify the configuration file. Modify directly in web.config as follows:

<sectionGroup name="spring">  <!--解析spring块的对象-->  <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>  <!--配置解析spring存放对象的容器集合-->  <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/></sectionGroup>
  </configSections><!--****************** Spring 配置开始 ******************-->
  <spring><context>  <!--容器配置,配置当前容器对象放在上面位置:当前是在现在的配置文件中-->  <resource uri="config://spring/objects" /><!--当前--></context><objects xmlns="http://www.springframework.net">  <!--这里存放容器所有节点-->  <description>An example that demonstrates simple Ioc features</description>  <!-- name 必须唯一 可以随意命名,一般为类型名称,type=类的全部名称,所在程序集,目的是为了让容器轻松的反射创建对象-->  <object name="UserInfo" type="SpringDemo.Models.UserInfo,SpringDemo"><property name="UserName" value="老王" /><!--ref 指向下面的属相注入--><property name="OrderBy" ref="Order" />  </object>  <object name="Order" type="SpringDemo.Models.Order,SpringDemo"><property name="OrderNo" value="20170808" />  </object></objects>
  </spring>
  <!--****************** Spring 配置结束 ******************-->

4. Code test, create a 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();
        }

5. Front desk Add display @ViewBag.msg and the running result is as follows:

6. The previous step is done, but what if you want to separate the configuration file? Create a new one in order to achieve the test effect. The code of class NewUserInfo is as follows:

public class NewUserInfo : IUserInfo
    {public NewUserInfo(string name, Order order)
        {this.UserName = name;this.OrderBy = order;
        }public string UserName { get; set; }public Order OrderBy { get; set; }public string ShowMeg()
        {return "姓名:" + UserName + "订单号:" + OrderBy.OrderNo;
        }
    }

7. Create objects.xml under the new folder Config and set the properties to generate the directory content as follows:

<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net">
  <!--这里存放容器所有节点-->
  <description>An example that demonstrates simple Ioc features</description>
  
  <!--构造函数注入-->
  <object name="NewUserInfo" type="SpringDemo.Models.NewUserInfo,SpringDemo"><constructor-arg index="0" value="张学友"/><constructor-arg index="1" ref="Order"/>
  </object>
  <!--复杂依赖注入-->
  <object name="Order" type="SpringDemo.Models.Order,SpringDemo"><property name="OrderNO" value="20170909"/>
  </object> 
  </objects>

8. Modify web.config and specify objects.xml as the resolution dependency

<resource uri="~/Config/objects.xml" /><!--指定文档-->

9. Create Action NewUserInfo under the Home controller. The code is as follows:

public ActionResult NewUserInfo()
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            IUserInfo lister = (IUserInfo)ctx.GetObject("NewUserInfo");
            ViewBag.msg = lister.ShowMeg();return View();
        }

10. Add @ViewBag.msg to the front desk. The running results are as follows:

Summary: IoC brings us a lot of convenience. When you feel that using a simple factory is not good, you can use IoC instead, and most of them support AOP.

Good luck to you

The above is the detailed content of Share an IoC introductory tutorial example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn