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

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - 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