forked from structuremap/structuremap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructorInstance.cs
283 lines (229 loc) · 8.93 KB
/
ConstructorInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using StructureMap.Construction;
using StructureMap.Graph;
using StructureMap.TypeRules;
using StructureMap.Util;
namespace StructureMap.Pipeline
{
public class ConstructorInstance : Instance, IConfiguredInstance, IStructuredInstance
{
private readonly Cache<string, Instance> _dependencies = new Cache<string, Instance>();
private readonly Plugin _plugin;
public ConstructorInstance(Type pluggedType)
: this(PluginCache.GetPlugin(pluggedType))
{
}
public ConstructorInstance(Plugin plugin)
{
_plugin = plugin;
_dependencies.OnMissing = key =>
{
if (_plugin.FindArgumentType(key).IsSimple())
{
throw new StructureMapException(205, key, Name);
}
return new DefaultInstance();
};
}
public ConstructorInstance(Type pluggedType, string name)
: this(pluggedType)
{
Name = name;
}
protected Plugin plugin { get { return _plugin; } }
void IConfiguredInstance.SetChild(string name, Instance instance)
{
SetChild(name, instance);
}
public void SetValue(Type type, object value, CannotFindProperty cannotFind)
{
string name = _plugin.FindArgumentNameForType(type, cannotFind);
if (name != null) SetValue(name, value);
}
void IConfiguredInstance.SetValue(string name, object value)
{
SetValue(name, value);
}
void IConfiguredInstance.SetCollection(string name, IEnumerable<Instance> children)
{
SetCollection(name, children);
}
public string GetProperty(string propertyName)
{
return _dependencies[propertyName].As<ObjectInstance>().Object.ToString();
}
public object Get(string propertyName, Type pluginType, BuildSession session)
{
return _dependencies[propertyName].Build(pluginType, session);
}
public T Get<T>(string propertyName, BuildSession session)
{
object o = Get(propertyName, typeof (T), session);
if (o == null) return default(T);
return (T) o;
}
public Type PluggedType { get { return _plugin.PluggedType; } }
public bool HasProperty(string propertyName, BuildSession session)
{
// TODO -- richer behavior
return _dependencies.Has(propertyName);
}
Instance IStructuredInstance.GetChild(string name)
{
return _dependencies[name];
}
Instance[] IStructuredInstance.GetChildArray(string name)
{
return _dependencies[name].As<EnumerableInstance>().Children.ToArray();
}
void IStructuredInstance.RemoveKey(string name)
{
_dependencies.Remove(name);
}
protected override bool canBePartOfPluginFamily(PluginFamily family)
{
return _plugin.PluggedType.CanBeCastTo(family.PluginType);
}
public ConstructorInstance Override(ExplicitArguments arguments)
{
var instance = new ConstructorInstance(_plugin);
_dependencies.Each((key, i) => instance.SetChild(key, i));
arguments.Configure(instance);
return instance;
}
protected override sealed string getDescription()
{
return "Configured Instance of " + _plugin.PluggedType.AssemblyQualifiedName;
}
protected override sealed Type getConcreteType(Type pluginType)
{
return _plugin.PluggedType;
}
internal void SetChild(string name, Instance instance)
{
if (instance == null)
{
throw new ArgumentNullException("instance", "Instance for {0} was null".ToFormat(name));
}
_dependencies[name] = instance;
}
internal void SetValue(string name, object value)
{
Type dependencyType = getDependencyType(name);
Instance instance = buildInstanceForType(dependencyType, value);
SetChild(name, instance);
}
private Type getDependencyType(string name)
{
Type dependencyType = _plugin.FindArgumentType(name);
if (dependencyType == null)
{
throw new ArgumentOutOfRangeException("name",
"Could not find a constructor parameter or property for {0} named {1}"
.ToFormat(_plugin.PluggedType.AssemblyQualifiedName, name));
}
return dependencyType;
}
internal void SetCollection(string name, IEnumerable<Instance> children)
{
Type dependencyType = getDependencyType(name);
var instance = new EnumerableInstance(dependencyType, children);
SetChild(name, instance);
}
protected string findPropertyName<PLUGINTYPE>()
{
Type dependencyType = typeof (PLUGINTYPE);
return findPropertyName(dependencyType);
}
protected string findPropertyName(Type dependencyType)
{
string propertyName = _plugin.FindArgumentNameForType(dependencyType, CannotFindProperty.ThrowException);
if (string.IsNullOrEmpty(propertyName))
{
throw new StructureMapException(305, dependencyType);
}
return propertyName;
}
private Instance buildInstanceForType(Type dependencyType, object value)
{
if (value == null) return new NullInstance();
if (dependencyType.IsSimple() || dependencyType.IsNullable() || dependencyType == typeof (Guid) ||
dependencyType == typeof (DateTime))
{
try
{
if (value.GetType() == dependencyType) return new ObjectInstance(value);
TypeConverter converter = TypeDescriptor.GetConverter(dependencyType);
object convertedValue = converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
return new ObjectInstance(convertedValue);
}
catch (Exception e)
{
throw new StructureMapException(206, e, Name);
}
}
return new ObjectInstance(value);
}
protected override object build(Type pluginType, BuildSession session)
{
IInstanceBuilder builder = PluginCache.FindBuilder(_plugin.PluggedType);
return Build(pluginType, session, builder);
}
public object Build(Type pluginType, BuildSession session, IInstanceBuilder builder)
{
if (builder == null)
{
throw new StructureMapException(
201, _plugin.PluggedType.FullName, Name, pluginType);
}
try
{
var args = new Arguments(this, session);
return builder.BuildInstance(args);
}
catch (StructureMapException)
{
throw;
}
catch (InvalidCastException ex)
{
throw new StructureMapException(206, ex, Name);
}
catch (Exception ex)
{
throw new StructureMapException(207, ex, Name, pluginType.FullName);
}
}
public static ConstructorInstance For<T>()
{
return new ConstructorInstance(typeof (T));
}
public override Instance CloseType(Type[] types)
{
if(!_plugin.PluggedType.IsOpenGeneric())
return null;
Type closedType;
try {
closedType = _plugin.PluggedType.MakeGenericType(types);
}
catch {
return null;
}
var closedInstance = new ConstructorInstance(closedType);
_dependencies.Each((key, i) => {
if(i.CopyAsIsWhenClosingInstance) {
closedInstance.SetChild(key, i);
}
});
return closedInstance;
}
public override string ToString()
{
return "'{0}' -> {1}".ToFormat(Name, _plugin.PluggedType.FullName);
}
}
}