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

推荐订阅源

云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
V
Visual Studio Blog
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
L
LangChain Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
博客园 - 司徒正美

博客园 - 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系列(5): 使用BuildUp让已存在对象实例也支持依赖注入 Unity Application Block 1.0系列(4): 方法调用注入(Method Call Injection )
Unity Application Block 1.0系列(6): 杜绝循环引用
Inrie · 2008-04-18 · via 博客园 - Inrie

public class Class1
{
    
public Class1(Class2 test2)
    {
        ..
    }
}
public class Class2
{
    
public Class2(Class1 test1)  
    {
        
    }
}

通过Unity容器装载Class1时,会做Constructor Injection,由于Class1依赖于Class2,所以会先去装载Class2。在装载Class2时同样也会做Constructor Injection,由于Class2又依赖于Class1,所以会再去装载Class1......这样一直持续装载下去,就成了一个死循环,跳不出来。

2. 通过Constructor Injection生成的对象作为自身构造器的参数直接看例子:

public class Class1
{
    
public Class1(Class1 test1)
  {  }
}

3. 通过method call injection生成的对象互相引用

直接看例子:

public class Class1
{
    [InjectionMethod]
    
public void Method1()
    {
        Method2();
    }

    [InjectionMethod]

public void Method2()
    {
        Method1();
    }
}


4.通过property(setter) injection生成的对象互相引用

直接看例子:

public class Class1
{
    [Dependency]
    
public string Propert1
    {
        
get
        {
            
return Propert2;
        }
    }

    [Dependency]

public string Propert2
    {
        
get
        {
            
return Propert1;
        }
    }
}

作者:Inrie (洪晓军)
出处:http://www.cnblogs.com/inrie