/
PluginFamily.cs
366 lines (284 loc) · 11 KB
/
PluginFamily.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System;
using System.Collections.Generic;
using System.Linq;
using StructureMap.Pipeline;
using StructureMap.TypeRules;
using StructureMap.Util;
namespace StructureMap.Graph
{
/// <summary>
/// Conceptually speaking, a PluginFamily object represents a point of abstraction or variability in
/// the system. A PluginFamily defines a CLR Type that StructureMap can build, and all of the possible
/// Plugin’s implementing the CLR Type.
/// </summary>
public class PluginFamily : IPluginFamily
{
private readonly Cache<string, Instance> _instances = new Cache<string, Instance>(delegate { return null; });
private readonly List<InstanceMemento> _mementoList = new List<InstanceMemento>();
private readonly Cache<string, Plugin> _pluggedTypes = new Cache<string, Plugin>();
private readonly Type _pluginType;
private string _defaultKey = string.Empty;
private ILifecycle _lifecycle;
private PluginGraph _parent;
public PluginFamily(Type pluginType)
: this(pluginType, new PluginGraph())
{
}
public PluginFamily(Type pluginType, PluginGraph parent)
{
_parent = parent;
_pluginType = pluginType;
PluginFamilyAttribute.ConfigureFamily(this);
}
public PluginGraph Parent { get { return _parent; } set { _parent = value; } }
public IEnumerable<Instance> Instances { get { return _instances.GetAll(); } }
#region IPluginFamily Members
public void AddMementoSource(MementoSource source)
{
_mementoList.AddRange(source.GetAllMementos());
}
public void SetScopeTo(InstanceScope scope)
{
if (scope == InstanceScope.Transient)
{
_lifecycle = null;
return;
}
_lifecycle = Lifecycles.GetLifecycle(scope);
}
#endregion
public void SetScopeTo(ILifecycle lifecycle)
{
_lifecycle = lifecycle;
}
public ILifecycle Lifecycle { get { return _lifecycle; } }
public void AddInstance(InstanceMemento memento)
{
_mementoList.Add(memento);
}
public void AddInstance(Instance instance)
{
_instances[instance.Name] = instance;
}
public void SetDefault(Instance instance)
{
AddInstance(instance);
DefaultInstanceKey = instance.Name;
}
// For testing
public InstanceMemento GetMemento(string instanceKey)
{
return _mementoList.Find(m => m.InstanceKey == instanceKey);
}
public void AddTypes(List<Type> pluggedTypes)
{
pluggedTypes.ForEach(type => AddType(type));
}
public void Seal()
{
_mementoList.ForEach(memento => _parent.Log.Try(() =>
{
Instance instance = memento.ReadInstance(Parent, _pluginType);
AddInstance(instance);
}).AndLogAnyErrors());
discoverImplicitInstances();
validatePluggabilityOfInstances();
if (_pluginType.IsConcrete() && PluginCache.GetPlugin(_pluginType).CanBeAutoFilled)
{
MissingInstance = new ConfiguredInstance(_pluginType);
}
if (_instances.Count == 1)
{
_defaultKey = _instances.First.Name;
}
}
private void validatePluggabilityOfInstances()
{
_instances.Each(instance =>
{
IDiagnosticInstance diagnosticInstance = instance;
_parent.Log.Try(() => diagnosticInstance.Preprocess(this))
.AndReportErrorAs(104, diagnosticInstance.CreateToken(), _pluginType);
if (!diagnosticInstance.CanBePartOfPluginFamily(this))
{
_parent.Log.RegisterError(104, diagnosticInstance.CreateToken(), _pluginType);
}
});
}
private void discoverImplicitInstances()
{
_pluggedTypes.Each((key, plugin) =>
{
if (!plugin.CanBeAutoFilled) return;
if (hasInstanceWithPluggedType(plugin)) return;
ConfiguredInstance instance = new ConfiguredInstance(plugin.PluggedType).WithName(key);
FillInstance(instance);
});
}
private void FillInstance(Instance instance)
{
_instances.Fill(instance.Name, instance);
}
private bool hasInstanceWithPluggedType(Plugin plugin)
{
return _instances.Exists(instance => instance.Matches(plugin));
}
public Instance GetInstance(string name)
{
return _instances[name];
}
public bool HasPlugin(Type pluggedType)
{
return _pluggedTypes.Exists(plugin => plugin.PluggedType == pluggedType);
}
private void assertPluggability(Type pluggedType)
{
if (!pluggedType.CanBeCastTo(_pluginType))
{
throw new StructureMapException(104, pluggedType, _pluginType);
}
if (!Constructor.HasConstructors(pluggedType))
{
throw new StructureMapException(180, pluggedType.AssemblyQualifiedName);
}
}
public Plugin AddPlugin(Type pluggedType)
{
assertPluggability(pluggedType);
Plugin plugin = PluginCache.GetPlugin(pluggedType);
_pluggedTypes[plugin.ConcreteKey] = plugin;
return plugin;
}
public Plugin AddPlugin(Type pluggedType, string key)
{
assertPluggability(pluggedType);
Plugin plugin = PluginCache.GetPlugin(pluggedType);
_pluggedTypes[key] = plugin;
return plugin;
}
public Instance GetDefaultInstance()
{
return string.IsNullOrEmpty(_defaultKey) ? null : GetInstance(_defaultKey);
}
public Plugin FindPlugin(Type pluggedType)
{
return _pluggedTypes.Find(p => p.PluggedType == pluggedType);
}
public void AddDefaultMemento(InstanceMemento memento)
{
if (string.IsNullOrEmpty(memento.InstanceKey))
{
memento.InstanceKey = "DefaultInstanceOf" + PluginType.AssemblyQualifiedName;
}
AddInstance(memento);
DefaultInstanceKey = memento.InstanceKey;
}
public void FillDefault(Profile profile)
{
if (string.IsNullOrEmpty(DefaultInstanceKey))
{
return;
}
Instance defaultInstance = GetInstance(DefaultInstanceKey);
if (defaultInstance == null)
{
Parent.Log.RegisterError(210, DefaultInstanceKey, PluginType);
}
profile.FillTypeInto(PluginType, defaultInstance);
}
public void ImportFrom(PluginFamily source)
{
source.Instances.Each(instance => _instances.Fill(instance.Name, instance));
source._pluggedTypes.Each((key, plugin) => _pluggedTypes.Fill(key, plugin));
if (source.MissingInstance != null)
{
MissingInstance = source.MissingInstance;
}
}
public Instance FirstInstance()
{
return _instances.First;
}
public Plugin FindPlugin(string concreteKey)
{
if (_pluggedTypes.Has(concreteKey))
{
return _pluggedTypes[concreteKey];
}
return null;
}
public bool HasPlugin(string concreteKey)
{
return _pluggedTypes.Has(concreteKey);
}
public PluginFamily CreateTemplatedClone(Type[] templateTypes)
{
Type templatedType = _pluginType.MakeGenericType(templateTypes);
var templatedFamily = new PluginFamily(templatedType, Parent);
templatedFamily.DefaultInstanceKey = DefaultInstanceKey;
templatedFamily._lifecycle = _lifecycle;
_instances.GetAll().Select(x =>
{
Instance clone = x.CloseType(templateTypes);
if (clone == null) return null;
clone.Name = x.Name;
return clone;
}).Where(x => x != null).Each(templatedFamily.AddInstance);
// Need to attach the new PluginFamily to the old PluginGraph
Parent.PluginFamilies.Add(templatedFamily);
return templatedFamily;
}
private bool hasType(Type concreteType)
{
return FindPlugin(concreteType) != null || _instances.Any(x => x.ConcreteType == concreteType);
}
public void AddType(Type concreteType)
{
if (!concreteType.CanBeCastTo(_pluginType)) return;
if (!hasType(concreteType))
{
AddPlugin(concreteType);
}
}
public void AddType(Type concreteType, string name)
{
if (!concreteType.CanBeCastTo(_pluginType)) return;
if (!hasType(concreteType))
{
AddPlugin(concreteType, name);
}
}
public void ForInstance(string name, Action<Instance> action)
{
_instances.WithValue(name, action);
}
public void RemoveInstance(Instance instance)
{
_instances.Remove(instance.Name);
if (_defaultKey == instance.Name)
{
_defaultKey = null;
}
}
public void RemoveAll()
{
_instances.Clear();
_mementoList.Clear();
_defaultKey = null;
}
#region properties
public bool IsGenericTemplate { get { return _pluginType.IsGenericTypeDefinition || _pluginType.ContainsGenericParameters; } }
public int PluginCount { get { return _pluggedTypes.Count; } }
public int InstanceCount { get { return _instances.Count; } }
public Instance MissingInstance { get; set; }
/// <summary>
/// The CLR Type that defines the "Plugin" interface for the PluginFamily
/// </summary>
public Type PluginType { get { return _pluginType; } }
/// <summary>
/// The InstanceKey of the default instance of the PluginFamily
/// </summary>
public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } }
#endregion
}
}