您好,欢迎访问一九零五行业门户网

分享一个IoC入门教程实例

spring.net包括控制反转(ioc) 和面向切面(aop),这篇文章主要说下ioc方面的入门。
一、首先建立一个mvc项目名称叫springdemo,然后用nuget下载spring(我用的是spring.net nhibernate 4 support)
二、类设计,在models文件夹下面建立类,主要iuserinfo,userinfo,order 三个类代码如下:
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; }     }
三、进入关键的一步:修改配置文件。在web.config里直接修改 如下:
<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 配置结束 ******************-->
四、代码测试,新建控制器,代码如下:
public actionresult index()         {             iapplicationcontext ctx = contextregistry.getcontext();             iuserinfo lister = (iuserinfo)ctx.getobject(userinfo);             viewbag.msg = lister.showmeg();return view();         }
五、前台添加显示 @viewbag.msg 运行结果如下:
六、前已经大功告成,但如果想把配置文件单独出来该怎么弄呢,为了达到测试效果新建一个类newuserinfo 代码如下:
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;         }     }
七、新建文件夹config下建立objects.xml 属性设置生成到目录 内容如下:
<?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>
八、修改web.config 指定objects.xml为解析依赖
<resource uri="~/config/objects.xml" /><!--指定文档-->
九、在home控制器下建立action newuserinfo 代码如下:
public actionresult newuserinfo()         {             iapplicationcontext ctx = contextregistry.getcontext();             iuserinfo lister = (iuserinfo)ctx.getobject(newuserinfo);             viewbag.msg = lister.showmeg();return view();         }
十、前台添加显示 @viewbag.msg 运行结果如下:
总结:ioc给我们带来了很多的方便,当我们觉得使用简单工厂不好的时候,可以用ioc来代替,而且大部分都支持aop。
祝君好运
以上就是分享一个ioc入门教程实例的详细内容。
其它类似信息

推荐信息