文章说明,文本内容基于配置文件进行依赖注入
unity介绍:Unity是由微软的Patterns & Practices团队开发的一个轻量级、可扩展的依赖注入(Dependency Injection, DI)容器,支持拦截、构造函数注入、属性注入和方法调用注入。
属性注入:
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<alias alias="Class" type="Unity.Class,Unity" />
<container>
<register type="Student" name="MyStudent">
<constructor>
<param name="name" type="System.String" value="BBBB"/>
<param name="Studentage" type="System.Int32" value="12"/>
</constructor>
</register>
<register type="Class" name="MyClass">
<!--属性注入 -->
<property name="ClassName" propertyType="System.String" value="电脑班" />
<!--对象属性注入-->
<property name="NewStudent" >
<dependency name="MyStudent" type="Unity.Student,Unity" />
</property>
</register>
</container>
<unity>
构造函数注入:
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<alias alias="Student" type="Unity.Student,Unity" />
<container>
<!--构造函数注入 -->
<register type="Student" name="MyStudent">
<constructor>
<param name="name" type="System.String" value="BBBB"/>
<param name="Studentage" type="System.Int32" value="12"/>
</constructor>
</register>
</container>
<unity>
方法注入:
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity>
<alias alias="Class" type="Unity.Class,Unity" />
<container>
<register type="Class" name="MyClass">
<method name="CreateTeacher">
<param name="person" type="IPerson">
<dependency type="Teacher"/>
</param>
</method>
</register>
</container>
<unity>
----------------------------------------
源码下载