上节介绍了,Unity
的Lifetime Managers
生命周期,Unity
具体实现依赖注入包含构造函数注入、属性注入、方法注入,所谓注入相当赋值,下面一个一个来介绍。
Unity
利用Resolve
方法解析一个对象,都是调用注册类型的构造函数来初始化的,初始化时,Unity
能够控制初始化的值,当然,我们要给Unity
提供足够的原料,要不然也是巧妇难无米之炊,下面看一些简单的示例。
先准备几个类如下:
///
/// 班级接口
///
public interface IClass
{
string ClassName { get; set; }
void ShowInfo();
}
///
/// 计科班
///
public class CbClass : IClass
{
public string ClassName { get; set; }
public void ShowInfo()
{
Console.WriteLine("计科班:{0}", ClassName);
}
}
///
/// 电商班
///
public class EcClass : IClass
{
public string ClassName { get; set; }
public void ShowInfo()
{
Console.WriteLine("电商班:{0}", ClassName);
}
}
///
/// 学生接口
///
public interface IStudent
{
string Name { get; set; }
//就读班级
void ShowInfo();
}
///
/// 学生
///
public class QlinStudent : IStudent
{
public string Name { get; set; }
private IClass ToClass { get; set; }
public QlinStudent(IClass _class)
{
ToClass = _class;
}
public void ShowInfo()
{
Console.WriteLine("{0}就读班级:{1}", Name, ToClass.ClassName);
}
}
是一个班级和学生的结构,现在我们要解析一个学生IStudent
,我们看到具体学生类QlinStudent
的构造函数需要一个班级接口,当然要给IUnityContainer
容器提供这个班级映射还有学生自己的映射,就你要什么东东,首先要提供IUnityContainer
什么东东。
默认方式跟new一个对象,它会根据你提供的材料,选择一个构造函数,即要有构造器要能访问权限,用Public
修饰,构造函数的参数也要提供,即IClass
也要能解析,不然就报错了,编程注入方式如下:
public static void ConStructorCodeTest1()
{
IUnityContainer container = new UnityContainer();
//默认注册(无命名),如果后面还有默认注册会覆盖前面的
container.RegisterType();
container.RegisterType();
//解析默认对象
IStudent splitClass = container.Resolve();
splitClass.ShowInfo();
}
配置文件方式 如下:
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<namespace name="ConsoleApplication1.UnityDemo.Constructor" />
<assembly name="ConsoleApplication1" />
<container name="FirstClass">
<register type="IClass" mapTo="CbClass">register>
<register type="IClass" name="ec" mapTo="EcClass">register>
<register type="IStudent" mapTo="QlinStudent">
register>
container>
unity>
configuration>
以下是加载配置文件
public static void ConStructorConfigTest1()
{
IUnityContainer container = new UnityContainer();
string configFile = "http://www.cnblogs.com/UnityDemo/Constructor/Unity.config";
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
//从config文件中读取配置信息
Configuration configuration =
ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
//获取指定名称的配置节
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection("unity");
//载入名称为FirstClass 的container节点
container.LoadConfiguration(section, "FirstClass");
IStudent splitClass = container.Resolve();
splitClass.ShowInfo();
}
如果构造函数有多个,它也会按照上面那样来初始化一个对象,我们还可以显示用InjectionConstructor
特性来指定一个构造函数来解析对象,如下声明:
public class QlinStudent : IStudent
{
private string Name { get; set; }
private IClass ToClass { get; set; }
public QlinStudent()
{
}
[InjectionConstructor]
public QlinStudent(IClass _class,string name)
{
ToClass = _class;
Name = name;
}
public void ShowInfo()
{
Console.WriteLine("{0}就读班级:{1}", Name, ToClass.ClassName);
}
}
构造函数中IClass参数,如果IUnityContainer注册了多个,默认是使用无名称的那个注册,也可以通过Dependency依赖哪个名称来指定哪个来注册,代码,指定ec名称如下:
[InjectionConstructor]
public QlinStudent([Dependency("ec")]IClass _class)
{
ToClass = _class;
}
下面注册一个名称为ec的映射,如果没有名称ec的映射将报错
public static void ConStructorCodeTest1()
{
IUnityContainer container = new UnityContainer();
//默认注册(无命名),如果后面还有默认注册会覆盖前面的
container.RegisterType();
//命名注册
container.RegisterType("ec");
container.RegisterType();
//解析默认对象
IStudent splitClass = container.Resolve();
splitClass.ShowInfo();
}
配置文件方式,代码不变,配置中添加一个 name
属性就行,如下:
<container name="FirstClass">
<register type="IClass" mapTo="CbClass">register>
<register type="IClass" name="ec" mapTo="EcClass">register>
<register type="IStudent" mapTo="QlinStudent">
register>
container>
构造器中的参数也可以依赖一个指定的类型值,如下代码依赖于EcClass
类型,可以让构造函数中可以传入一个具体的类型,这也是构造函数传参数,如下:
public static void ConStructorCodeTest1()
{
IUnityContainer container = new UnityContainer();
//默认注册(无命名),如果后面还有默认注册会覆盖前面的
container.RegisterType();
//命名注册
container.RegisterType("ec");
container.RegisterType(new InjectionConstructor(new CbClass()));
IStudent splitClass = container.Resolve();
splitClass.ShowInfo();
}
或者注册一个实例对象,如下:
public static void ConStructorCodeTest1()
{
IUnityContainer container = new UnityContainer();
IClass cbClass = new CbClass { ClassName="计科051班" };
//实例注册命名实例
container.RegisterInstance("ec", cbClass);
container.RegisterType();
IStudent splitClass = container.Resolve();
splitClass.ShowInfo();
}
配置文件也可以指定类型依赖,如下,指定EcClass
:
<register type="IStudent" mapTo="QlinStudent">
<constructor>
<param name="_class" type="IClass">
<dependency type="EcClass"/>
param>
constructor>
register>
上面已经介绍了传参数,是用InjectionConstructor
类型,现在构造函数,多一个参数,如下:
[InjectionConstructor]
public QlinStudent([Dependency("ec")]IClass _class, string name)
{
ToClass = _class;
Name = name;
}
多了一个name
参数,那必须为容器IUnityContainer
提供这个参数,没有这个原材料,它无法构造,就会报错,如下代码:
public static void ConStructorCodeTest1()
{
IUnityContainer container = new UnityContainer();
container.RegisterType(new InjectionConstructor(new CbClass() { ClassName = "计科051" }, "Qlin"));
IStudent splitClass = container.Resolve();
splitClass.ShowInfo();
}
注入参数后,也可以下次解析的时候,通过ParameterOverrides
类来覆盖原来的参数,改变参数值,如下:
public static void ConStructorCodeTest1()
{
IUnityContainer container = new UnityContainer();
container.RegisterType(new InjectionConstructor(new CbClass() { ClassName = "计科051" }, "Qlin"));
IStudent student = container.Resolve();
student.ShowInfo();
//覆盖参数解析
IStudent student1 = container.Resolve(new ParameterOverrides()
{
{"_class",new EcClass(){ ClassName="电商051"}},
{"name","linq"}
});
student1.ShowInfo();
}
就是Unity
容器解析对象时,为属性赋值,有操作权限要Public
修饰属性。属性注入方式和构造函数注入类似,只需在需要注入的属性上增加一个Dependency
特性,Dependency
指定一个注册名称name
参数用来指定注入对象的名称,属性注入也是伴随着类型初始化时注入的,在解析时自动注入,所以解析时跟以前一样。代码修改如下,在ToClass
属性上增加了Dependency
特性,来表示这个属性需要注入:
public class QlinStudent : IStudent
{
public string Name { get; set; }
[Dependency("ec")]
public IClass ToClass { get; set; }
public void ShowInfo()
{
Console.WriteLine("{0}就读班级:{1}", Name, ToClass.ClassName);
}
}
代码方式如下:
IUnityContainer container = new UnityContainer();
container.RegisterType("ec");
container.RegisterType();
IStudent splitClass = container.Resolve();
splitClass.ShowInfo();
配置文件方式,依赖的
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<namespace name="ConsoleApplication1.UnityDemo.Constructor4" />
<assembly name="ConsoleApplication1" />
<container name="FirstClass">
<register type="IClass" mapTo="CbClass">
register>
<register type="IClass" name="ec1" mapTo="EcClass">
<property name="ClassName" propertyType="System.String" value="电商051" />
register>
<register type="IStudent" mapTo="QlinStudent">
<property name="ToClass">
<dependency name="ec1" type="EcClass"/>
property>
register>
container>
unity>
调用效果图:
用public
修饰方法,方法注入也是跟构造函数类似代码修改如下
public class QlinStudent : IStudent
{
public string Name { get; set; }
private IClass ToClass { get; set; }
[InjectionMethod]
public void InitClass(IClass _class)
{
ToClass = _class;
}
public void ShowInfo()
{
Console.WriteLine("{0}就读班级:{1}", Name, ToClass.ClassName);
}
}
编程方式注入不变,就是初始化时,注入值,如下:
IUnityContainer container = new UnityContainer();
container.RegisterType();
container.RegisterType();
IStudent student = container.Resolve();
student.ShowInfo();
配置文件方式:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<namespace name="ConsoleApplication1.UnityDemo.Constructor5" />
<assembly name="ConsoleApplication1" />
<container name="FirstClass">
<register type="IClass" mapTo="CbClass">
register>
<register type="IClass" name="ec1" mapTo="EcClass">
<property name="ClassName" propertyType="System.String" value="电商051" />
register>
<register type="IStudent" mapTo="QlinStudent">
<property name="Name" propertyType="System.String" value="Qlin" />
<method name="InitClass">
<param name="_class" type="IClass">
<dependency name="ec1" type="EcClass"/>
param>
method>
register>
container>
unity>
介绍了3种依赖注入方式,平时主要也就用到这么几种,其它还有复杂的像扩展容器等,通过本节,基本知道Unity的使用了。