首页 新闻 赞助 找找看

IOC 中的依赖对象和被依赖对象怎么理解?

0
[已关闭问题] 关闭于 2018-03-29 08:59
 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             IUnityContainer container = new UnityContainer();
 6             UnityConfigurationSection configuration = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
 7             configuration.Configure(container, "defaultContainer");
 8             A a = container.Resolve<IA>() as A;
 9             if (null != a)
10             {
11                 Console.WriteLine("a.B == null ? {0}", a.B == null ? "Yes" : "No");
12                 Console.WriteLine("a.C == null ? {0}", a.C == null ? "Yes" : "No");
13                 Console.WriteLine("a.D == null ? {0}", a.D == null ? "Yes" : "No");
14             }
15 
16         }
17     }
18 
19     public interface IA { }
20     public interface IB { }
21     public interface IC { }
22     public interface ID { }
23 
24     public class A : IA
25     {
26         public IB B { get; set; }
27         [Dependency]
28         public IC C { get; set; }
29         public ID D { get; set; }
30 
31         public A(IB b)
32         {
33             this.B = b;
34         }
35         [InjectionMethod]
36         public void Initialize(ID d)
37         {
38             this.D = d;
39         }
40     }
41     public class B : IB { }
42     public class C : IC { }
43     public class D : ID { }

最近在学习IOC相关的知识,书上对属性注入的解释是:如果需要使用到被依赖对象的某个属性,在被依赖对象呗创建之后,IoC容器会自动初始化该属性。

在上面的代码里 Class A 中的属性C是,那么这里被依赖的对象是只谁?

Cornelius的主页 Cornelius | 初学一级 | 园豆:80
提问于:2014-11-15 10:18
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册