惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
I
InfoQ
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
C
Cybersecurity and Infrastructure Security Agency CISA
Know Your Adversary
Know Your Adversary
MongoDB | Blog
MongoDB | Blog
T
Tor Project blog
The Register - Security
The Register - Security
H
Help Net Security
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
B
Blog RSS Feed
Latest news
Latest news
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
C
Cisco Blogs
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
S
Schneier on Security
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
W
WeLiveSecurity

博客园 - Inrie

[原创]MongoDB、HandlerSocket和MySQL性能测试及其结果分析 [原创]HandlerSocket系列(三):性能及其性能优化 [原创]HandlerSocket系列(二):架构、特点及其应用场景 [原创]HandlerSocket系列(一):由来 SD2.0 2009大会一些感想 在Windows Server 2008集群上做Sql Server 2008集群 解决Azure Project中使用Asp.net MVC RC会让VS崩溃的补丁 [译]剖析ASP.Net MVC Application 让VS 2008也可以用Mobile Web Form 几个不错的WCF Tools ADO.NET Entity Framework Beta3的一些问题 善待你的眼睛-使用微软专为程序员设计的Consolas字体 [推荐]ASP.NET 应用程序的扩展策略 [翻译+推荐]你需要知道的:WCF、WF、ADO.NET SyncServices和ClickOnce 如何发布Ado.net Entity Framework EDM Unity Application Block 1.0系列文章 Unity Application Block 1.0系列(7): Lifetime Managers Unity Application Block 1.0系列(6): 杜绝循环引用 Unity Application Block 1.0系列(5): 使用BuildUp让已存在对象实例也支持依赖注入
Unity Application Block 1.0系列(4): 方法调用注入(Method Call Injection )
Inrie · 2008-04-18 · via 博客园 - Inrie

什么情况下使用Method Call Injection当实例化父对象时也能自动实例化所依赖的对象
通过简单的方式使得很容易做到在代码中查看每个类所依赖的项
父对象有很多相互之间有关联关系的构造器,导致在调试和维护时很不方便
父对象包含有很多参数构造器,特别是参数类型相似的只能通过参数的位置来辨别的
隐藏依赖的对象,不作为属性暴露出去
通过修改依赖对象的代码来控制哪些对象可以被注入,而不用改动父对象或应用程序

准备工作

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";
        }
    }
}


开始Constructor Injection在容器创建对象实例时就会触发执行,而Method Call Injection在具体调用对象实例的方法时候才触发。

通过为类的方法贴上[InjectionMethod]标签,使得Unity容器在获取类对象实例时,自动实例化该方法所依赖的对象,注入到该方法中。

看一个例子:

Mp3Player类中为Init(Song song)方法贴上了[InjectionMethod]标签:

[InjectionMethod]
public void
 Init(Song song)
{
  
this.mSong =
 song;
}


可以通过下面的方式来获取Mp3Player对象实例:

IUnityContainer container = new UnityContainer();

container.RegisterType

<IPlayer, Mp3Player>();

IPlayer player 

= container.Resolve<IPlayer>
();
player.Play();


输出:

Unity 3-4.jpg

这里通过为Mp3Player类的Init方法贴上[InjectionMethod]标签,来表示Unity容器装载Mp3Player对象时将自动实例化所依赖的对象(即Song对象),然后注入到Mp3Player的Init方法里 (执行该方法)。


即这里主要做两个操作:

1. Song song = new Song();

2. this.mSong = song;


注入到已存在的对象实例用Resolve方法来获取已存在的对象实例时不会做 Property Injection,因为该对象的创建没受到 Unity 容器的任何影响。可以使用BuildUp方法来强制实现 Property Injection。

关于BuildUp方法可参考:
Unity Application Block 1.0系列(5): 使用BuildUp让对象实例也支持依赖注入

结束语使用 Method Call Injection 需要特别注意不要有循环引用,否则可能会导致应用程序出错,至于循环引用的具体说明会有专门一篇文章介绍。

作者:Inrie (洪晓军)

出处:http://www.cnblogs.com/inrie