Unity Application Block 1.0系列(5): 使用BuildUp让已存在对象实例也支持依赖注入
通过Unity容器的BuildUp方法可以让已存在的对象实例也可以支持依赖注入。
准备工作
先看一些类,下面会用到:
public class Owner
{
public string Name
{
get
{
return "Inrie";
}
}
public int Age
{
get
{
return 24;
}
}
}
public abstract class Player
{
public abstract void Play();
public abstract string Name{get;}
[Dependency]
public Owner Owner { get; set; }
}
public class Mp3Player : Player
{
public override void Play()
{
Console.WriteLine("Playing Mp3");
}
public override string Name
{
get
{
return "Mp3 Player";
}
}
}
public class CDPlayer : Player
{
public override void Play()
{
Console.WriteLine("Playing CD");
}
public override string Name
{
get
{
return "CD Player";
}
}
}
public class DVDPlayer : Player
{
public override void Play()
{
Console.WriteLine("Playing DVD");
}
public override string Name
{
get
{
return "DVD Player";
}
}
}
{
public string Name
{
get
{
return "Inrie";
}
}
public int Age
{
get
{
return 24;
}
}
}
public abstract class Player
{
public abstract void Play();
public abstract string Name{get;}
[Dependency]
public Owner Owner { get; set; }
}
public class Mp3Player : Player
{
public override void Play()
{
Console.WriteLine("Playing Mp3");
}
public override string Name
{
get
{
return "Mp3 Player";
}
}
}
public class CDPlayer : Player
{
public override void Play()
{
Console.WriteLine("Playing CD");
}
public override string Name
{
get
{
return "CD Player";
}
}
}
public class DVDPlayer : Player
{
public override void Play()
{
Console.WriteLine("Playing DVD");
}
public override string Name
{
get
{
return "DVD Player";
}
}
}
开始
考虑下面的情况:
在系统的某个组件或类库中有个方法,该方法返回一个对象实例。
public class CoreLib
{
public Player CreatePlayer()
{
return new Mp3Player();
}
}
{
public Player CreatePlayer()
{
return new Mp3Player();
}
}
在程序中这样来调用此方法,获取对象实例,并输出该播放器拥有者名字
CoreLib coreLib = new CoreLib();
Player player = coreLib.CreatePlayer();
Console.WriteLine(player.Owner.Name);
Player player = coreLib.CreatePlayer();
Console.WriteLine(player.Owner.Name);
显而易见,这里肯定会报错,因为player对象的Owner属性依赖于Owner类,而这里在还没实例化该类时就想调用它的Name属性,会出现“未将对象引用设置到对象的实例”的错误。
大家应该可以马上想到,这正是属性注入出现的时候了。
这里为Player类的Owner属性加上[Dependency]标签(Attribute)。
[Dependency]
public Owner Owner { get; set; }
public Owner Owner { get; set; }
在Unity Application Block 1.0系列(3): 属性注入 中对属性注入做了较详细的说明。通过为属性加上[Dependency]标签, 然后通过Unity容器的Resolve方法自动做依赖注入,获取对象的实例。但是这种做法有个前提,就是对象的创建需要在我们的控制范围之内,也就是说这个对象需要在程序中创建(Resolve方法就是返回一个对象实例),而不是组件或类库中就创建好了。
对于这种情况,Unity提供了BuildUp方法来实现这样的需求。
BuildUp有以下几个重载方法:
BuildUp<T>(T existing)
BuildUp<T>(T existing, string name)
BuildUp(Type t, object existing)
BuildUp(Type t, object existing, string name)
还是实现上例中输出播放器拥有者名字的需求,把那段代码改为:
IUnityContainer container = new UnityContainer();
CoreLib coreLib = new CoreLib();
Player player = container.BuildUp<Player>(coreLib.CreatePlayer());
Console.WriteLine(player.Owner.Name);
CoreLib coreLib = new CoreLib();
Player player = container.BuildUp<Player>(coreLib.CreatePlayer());
Console.WriteLine(player.Owner.Name);
输出为:
可以看出BuildUp方法已经自动为我们做了依赖注入了。
结束语:
从上面例子中可以看到,需要为 Player 类的 Owner 属性贴上 [Dependency] 标签 ( Attribute ) ,这样的话在组件或类库中可能会充斥一些类似[Dependency]这样的东西,这些只是为了用来为Unity服务,而没有任何其他用途。相对于Castle IOC容器和Spring.Net来说,Unity的侵入性更强。
作者:Inrie (洪晓军)
出处:http://www.cnblogs.com/inrie