首页 / 软件开发 / .NET编程技术 / Unity Application Block 1.0系列(4):方法调用注入(Method Call Injection)
Unity Application Block 1.0系列(4):方法调用注入(Method Call Injection)2010-07-09 cnblogs inrie Inrie什么情况下使用Method CallInjection当实例化父对象时也能自动实例化所依赖的对象通过简单的方式使得很容易做到在代码中查看每个类所依赖的项父对象有很多相互之间有关联关系的构造器,导致在调试和维护时很不方便父对象包含有很多参数构造器,特别是参数类型相似的只能通过参数的位置来辨别的隐藏依赖的对象,不作为属性暴露出去通过修改依赖对象的代码来控制哪些对象可以被注入,而不用改动父对象或应用程序准备工作public interface IPlayer
{
void Play();
}
public class Mp3Player : IPlayer
{
public Song mSong;
[InjectionMethod]
public void Init(Song song)
{
this.mSong = song;
}
public void Play()
{
Console.WriteLine(string.Format("{0}: Now Playing [{1}] Singing by ({2})", this.Name, this.mSong.Name, this.mSong.Singer));
}
public string Name
{
get
{
return "Mp3 Player";
}
}
}