/
ConstructorInstance.cs
241 lines (200 loc) · 8.18 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
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using StructureMap.Building;
using StructureMap.Diagnostics;
using StructureMap.Graph;
using StructureMap.TypeRules;
namespace StructureMap.Pipeline
{
public class ConstructorInstance : ConstructorInstance<ConstructorInstance>
{
public ConstructorInstance(Type concreteType) : base(concreteType)
{
}
public ConstructorInstance(Type pluggedType, string name) : base(pluggedType, name)
{
}
protected override ConstructorInstance thisInstance
{
get { return this; }
}
protected override ConstructorInstance thisObject()
{
return this;
}
}
public abstract class ConstructorInstance<TThis> : ExpressedInstance<TThis>, IConfiguredInstance
where TThis : ConstructorInstance<TThis>
{
private readonly Type _pluggedType;
private readonly DependencyCollection _dependencies = new DependencyCollection();
public ConstructorInstance(Type concreteType)
{
if (!concreteType.IsConcrete())
{
throw new ArgumentOutOfRangeException("concreteType","Only concrete types can be built by ConstructorInstance");
}
if (!concreteType.GetConstructors().Any())
{
throw new ArgumentOutOfRangeException(
"{0} must have at least one public constructor to be plugged in by StructureMap".ToFormat(
concreteType.GetFullName()));
}
_pluggedType = concreteType;
_pluggedType.GetTypeInfo().ForAttribute<InstanceAttribute>(x => x.Alter(this));
}
public ConstructorInstance(Type pluggedType, string name)
: this(pluggedType)
{
Name = name;
}
/// <summary>
/// Explicitly select a constructor
/// </summary>
public ConstructorInfo Constructor { get; set; }
public Type PluggedType
{
get { return _pluggedType; }
}
public override IDependencySource ToBuilder(Type pluginType, Policies policies)
{
var plan = ConcreteType.BuildSource(_pluggedType, Constructor, _dependencies, policies);
if (!plan.IsValid())
{
var message = "Unable to create a build plan for concrete type " + PluggedType.GetFullName();
var visualizer = new BuildPlanVisualizer(null, levels: 0);
plan.AcceptVisitor(visualizer);
var writer = new StringWriter();
visualizer.Write(writer);
throw new StructureMapBuildPlanException(message)
{
Context = writer.ToString()
};
}
return plan;
}
public ConstructorInstance Override(ExplicitArguments arguments)
{
var instance = new ConstructorInstance(_pluggedType);
_dependencies.CopyTo(instance._dependencies);
arguments.Configure(instance);
return instance;
}
public override string Description
{
get
{
return HasExplicitName()
? "{0} ('{1}')".ToFormat(_pluggedType.GetFullName(), Name)
: _pluggedType.GetFullName();
}
}
public override IDependencySource ToDependencySource(Type pluginType)
{
return new LifecycleDependencySource(pluginType, this);
}
public override Type ReturnedType
{
get { return _pluggedType; }
}
public static ConstructorInstance For<T>()
{
return new ConstructorInstance(typeof (T));
}
public override Instance CloseType(Type[] types)
{
if (!_pluggedType.IsOpenGeneric())
return null;
Type closedType;
try
{
closedType = _pluggedType.MakeGenericType(types);
}
catch
{
return null;
}
var closedInstance = new ConstructorInstance(closedType);
_dependencies.Each(arg => closedInstance._dependencies.Add(arg.CloseType(types)));
return closedInstance;
}
public DependencyCollection Dependencies
{
get { return _dependencies; }
}
public override string ToString()
{
return "'{0}' -> {1}".ToFormat(Name, _pluggedType.FullName);
}
/// <summary>
/// Inline definition of a constructor dependency. Select the constructor argument by type. Do not
/// use this method if there is more than one constructor arguments of the same type
/// </summary>
/// <typeparam name="TCtorType"></typeparam>
/// <returns></returns>
public DependencyExpression<TThis, TCtorType> Ctor<TCtorType>()
{
return Ctor<TCtorType>(null);
}
/// <summary>
/// Inline definition of a constructor dependency. Select the constructor argument by type and constructor name.
/// Use this method if there is more than one constructor arguments of the same type
/// </summary>
/// <typeparam name="TCtorType"></typeparam>
/// <param name="constructorArg"></param>
/// <returns></returns>
public DependencyExpression<TThis, TCtorType> Ctor<TCtorType>(string constructorArg)
{
return new DependencyExpression<TThis, TCtorType>(thisObject(), constructorArg);
}
protected abstract TThis thisObject();
/// <summary>
/// Inline definition of a setter dependency. Only use this method if there
/// is only a single property of the TSetterType
/// </summary>
/// <typeparam name="TSetterType"></typeparam>
/// <returns></returns>
public DependencyExpression<TThis, TSetterType> Setter<TSetterType>()
{
return Ctor<TSetterType>();
}
/// <summary>
/// Inline definition of a setter dependency. Only use this method if there
/// is only a single property of the TSetterType
/// </summary>
/// <typeparam name="TSetterType"></typeparam>
/// <param name="setterName">The name of the property</param>
/// <returns></returns>
public DependencyExpression<TThis, TSetterType> Setter<TSetterType>(string setterName)
{
return Ctor<TSetterType>(setterName);
}
/// <summary>
/// Inline definition of a dependency on an Array of the CHILD type. I.e. CHILD[].
/// This method can be used for either constructor arguments or setter properties
/// </summary>
/// <typeparam name="TElement"></typeparam>
/// <returns></returns>
public ArrayDefinitionExpression<TThis, TElement> EnumerableOf<TElement>()
{
if (typeof (TElement).IsArray)
{
throw new ArgumentException("Please specify the element type in the call to TheArrayOf");
}
return new ArrayDefinitionExpression<TThis, TElement>(thisObject(), null);
}
/// <summary>
/// Inline definition of a dependency on an Array of the CHILD type and the specified setter property or constructor argument name. I.e. CHILD[].
/// This method can be used for either constructor arguments or setter properties
/// </summary>
/// <typeparam name="TElement"></typeparam>
/// <param name="ctorOrPropertyName"></param>
/// <returns></returns>
public ArrayDefinitionExpression<TThis, TElement> EnumerableOf<TElement>(string ctorOrPropertyName)
{
return new ArrayDefinitionExpression<TThis, TElement>(thisObject(), ctorOrPropertyName);
}
}
}