1using System;
2using System.Globalization;
3using Microsoft.Practices.ObjectBuilder.Properties;
4
5namespace Microsoft.Practices.ObjectBuilder
6{
7 /**//// <summary>
8 /// 该类用于提供获取
9 /// "依赖对象" 的功
10 /// 能。
11 /// </summary>
12 /// <remarks>
13 /// 该类只在 "创建
14 /// 上下文" 提供的
15 /// "定位器" 中查
16 /// 找 "依赖对象"。
17 /// </remarks>
18 public class DependencyResolver
19 {
20
21 字段#region 字段
22 /**//// <summary>
23 /// 创建上下文,用于
24 /// 获取 "依赖对象"。
25 /// </summary>
26 private IBuilderContext _context;
27 #endregion
28
29 构造函数#region 构造函数
30 /**//// <summary>
31 /// 初始化 <see cref="DependencyResolver"/> 类新实例。
32 /// </summary>
33 /// <param name="context">
34 /// 创建上下文,<see cref="DependencyResolver"/>
35 /// 类将从该 "上下文"
36 /// 中获取 "依赖对象"。
37 /// </param>
38 public DependencyResolver(IBuilderContext context)
39 {
40 if (context == null)
41 {
42 throw new ArgumentNullException("context");
43 }
44
45 _context = context;
46 }
47 #endregion
48
49 公有方法#region 公有方法
50 /**//// <summary>
51 /// 获取 "依赖对象"。
52 /// </summary>
53 /// <param name="typeToResolve">
54 /// 要获取的 "依赖
55 /// 对象" 的类型。
56 /// </param>
57 /// <param name="typeToCreate">
58 /// 当缺少 "依赖对
59 /// 象" 时(并且 <paramref name="notPresent"/>
60 /// 值为 <see cref="NotPresentBehavior.CreateNew"/>
61 /// ),要创建的新
62 /// "依赖对象" 的
63 /// 类型。
64 /// </param>
65 /// <param name="id">
66 /// 要获取的 "依赖
67 /// 对象"的 Id。传
68 /// 递 null 表示匿
69 /// 名的 "依赖对象"。
70 /// </param>
71 /// <param name="notPresent">
72 /// 表示当缺少 "依赖
73 /// 对象" 时执行的动
74 /// 作。
75 /// </param>
76 /// <param name="searchMode">
77 /// 表示 "搜索" 是否
78 /// 向上回溯的标志。
79 /// </param>
80 /// <returns>
81 /// 要获取的 "依赖对
82 /// 象" 。如果 "依赖
83 /// 对象" 没有找到并
84 /// 且 <paramref name="notPresent"/>
85 /// 的参数值为 <see cref="NotPresentBehavior.ReturnNull"/>,
86 /// 则返回 null。
87 /// </returns>
88 /// <remarks>
89 /// 代码流程:
90 ///
91 /// 1、如果没有指定 typeToCreate
92 /// 参数(值为 null),
93 /// 则认为该参数的值
94 /// 与要获取的 "依赖
95 /// 对象" 的类型一致。
96 ///
97 /// 2、使用指定的 "搜索
98 /// 模式" 在 "定位器"
99 /// 中查找 "依赖对象"。
100 ///
101 /// 3、如果没有在 "定位
102 /// 器" 中找到 "依赖
103 /// 对象", 则根据 notPresent
104 /// 参数的值执行相应
105 /// 的动作。
106 /// </remarks>
107 public object Resolve(
108 Type typeToResolve,
109 Type typeToCreate,
110 string id,
111 NotPresentBehavior notPresent,
112 SearchMode searchMode)
113 {
114 if (typeToResolve == null)
115 {
116 throw new ArgumentNullException("typeToResolve");
117 }
118
119 if (!Enum.IsDefined(typeof(NotPresentBehavior), notPresent))
120 {
121 throw new ArgumentException(Resources.InvalidEnumerationValue, "notPresent");
122 }
123
124 if (typeToCreate == null)
125 {
126 typeToCreate = typeToResolve;
127 }
128
129 DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeToResolve, id);
130
131 /**//*
132 * 首先,在 "定位器"
133 * 中查找 "依赖对象",
134 * 如果找到,则返回
135 * 该 "依赖对象"。
136 */
137 if (_context.Locator.Contains(key, searchMode))
138 {
139 return _context.Locator.Get(key, searchMode);
140 }
141
142 /**//*
143 * 如果走到这里,则
144 * 表示没有找到 "依
145 * 赖对象"。
146 */
147 switch (notPresent)
148 {
149 case NotPresentBehavior.CreateNew:
150
151 /**//*
152 * 创建新的 "依赖对
153 * 象"。
154 */
155 return _context.HeadOfChain.BuildUp(
156 _context,
157 typeToCreate,
158 null, // null 表示创建新的 "依赖对象"。
159 key.ID);
160
161 case NotPresentBehavior.ReturnNull:
162 return null;
163 default:
164 throw new DependencyMissingException(string.Format(CultureInfo.CurrentCulture,Resources.DependencyMissing,typeToResolve.ToString()));
165 }
166 }
167 #endregion
168
169 }
170}
171
2using System.Globalization;
3using Microsoft.Practices.ObjectBuilder.Properties;
4
5namespace Microsoft.Practices.ObjectBuilder
6{
7 /**//// <summary>
8 /// 该类用于提供获取
9 /// "依赖对象" 的功
10 /// 能。
11 /// </summary>
12 /// <remarks>
13 /// 该类只在 "创建
14 /// 上下文" 提供的
15 /// "定位器" 中查
16 /// 找 "依赖对象"。
17 /// </remarks>
18 public class DependencyResolver
19 {
20
21 字段#region 字段
22 /**//// <summary>
23 /// 创建上下文,用于
24 /// 获取 "依赖对象"。
25 /// </summary>
26 private IBuilderContext _context;
27 #endregion
28
29 构造函数#region 构造函数
30 /**//// <summary>
31 /// 初始化 <see cref="DependencyResolver"/> 类新实例。
32 /// </summary>
33 /// <param name="context">
34 /// 创建上下文,<see cref="DependencyResolver"/>
35 /// 类将从该 "上下文"
36 /// 中获取 "依赖对象"。
37 /// </param>
38 public DependencyResolver(IBuilderContext context)
39 {
40 if (context == null)
41 {
42 throw new ArgumentNullException("context");
43 }
44
45 _context = context;
46 }
47 #endregion
48
49 公有方法#region 公有方法
50 /**//// <summary>
51 /// 获取 "依赖对象"。
52 /// </summary>
53 /// <param name="typeToResolve">
54 /// 要获取的 "依赖
55 /// 对象" 的类型。
56 /// </param>
57 /// <param name="typeToCreate">
58 /// 当缺少 "依赖对
59 /// 象" 时(并且 <paramref name="notPresent"/>
60 /// 值为 <see cref="NotPresentBehavior.CreateNew"/>
61 /// ),要创建的新
62 /// "依赖对象" 的
63 /// 类型。
64 /// </param>
65 /// <param name="id">
66 /// 要获取的 "依赖
67 /// 对象"的 Id。传
68 /// 递 null 表示匿
69 /// 名的 "依赖对象"。
70 /// </param>
71 /// <param name="notPresent">
72 /// 表示当缺少 "依赖
73 /// 对象" 时执行的动
74 /// 作。
75 /// </param>
76 /// <param name="searchMode">
77 /// 表示 "搜索" 是否
78 /// 向上回溯的标志。
79 /// </param>
80 /// <returns>
81 /// 要获取的 "依赖对
82 /// 象" 。如果 "依赖
83 /// 对象" 没有找到并
84 /// 且 <paramref name="notPresent"/>
85 /// 的参数值为 <see cref="NotPresentBehavior.ReturnNull"/>,
86 /// 则返回 null。
87 /// </returns>
88 /// <remarks>
89 /// 代码流程:
90 ///
91 /// 1、如果没有指定 typeToCreate
92 /// 参数(值为 null),
93 /// 则认为该参数的值
94 /// 与要获取的 "依赖
95 /// 对象" 的类型一致。
96 ///
97 /// 2、使用指定的 "搜索
98 /// 模式" 在 "定位器"
99 /// 中查找 "依赖对象"。
100 ///
101 /// 3、如果没有在 "定位
102 /// 器" 中找到 "依赖
103 /// 对象", 则根据 notPresent
104 /// 参数的值执行相应
105 /// 的动作。
106 /// </remarks>
107 public object Resolve(
108 Type typeToResolve,
109 Type typeToCreate,
110 string id,
111 NotPresentBehavior notPresent,
112 SearchMode searchMode)
113 {
114 if (typeToResolve == null)
115 {
116 throw new ArgumentNullException("typeToResolve");
117 }
118
119 if (!Enum.IsDefined(typeof(NotPresentBehavior), notPresent))
120 {
121 throw new ArgumentException(Resources.InvalidEnumerationValue, "notPresent");
122 }
123
124 if (typeToCreate == null)
125 {
126 typeToCreate = typeToResolve;
127 }
128
129 DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeToResolve, id);
130
131 /**//*
132 * 首先,在 "定位器"
133 * 中查找 "依赖对象",
134 * 如果找到,则返回
135 * 该 "依赖对象"。
136 */
137 if (_context.Locator.Contains(key, searchMode))
138 {
139 return _context.Locator.Get(key, searchMode);
140 }
141
142 /**//*
143 * 如果走到这里,则
144 * 表示没有找到 "依
145 * 赖对象"。
146 */
147 switch (notPresent)
148 {
149 case NotPresentBehavior.CreateNew:
150
151 /**//*
152 * 创建新的 "依赖对
153 * 象"。
154 */
155 return _context.HeadOfChain.BuildUp(
156 _context,
157 typeToCreate,
158 null, // null 表示创建新的 "依赖对象"。
159 key.ID);
160
161 case NotPresentBehavior.ReturnNull:
162 return null;
163 default:
164 throw new DependencyMissingException(string.Format(CultureInfo.CurrentCulture,Resources.DependencyMissing,typeToResolve.ToString()));
165 }
166 }
167 #endregion
168
169 }
170}
171