-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathParameterConventionExtensions.cs
More file actions
229 lines (187 loc) · 8.04 KB
/
Copy pathParameterConventionExtensions.cs
File metadata and controls
229 lines (187 loc) · 8.04 KB
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
namespace SimpleInjector.CodeSamples
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using SimpleInjector;
using SimpleInjector.Advanced;
public interface IParameterConvention
{
bool CanResolve(InjectionTargetInfo target);
Expression BuildExpression(InjectionConsumerInfo consumer);
}
public static class ParameterConventionExtensions
{
public static void RegisterParameterConvention(this ContainerOptions options,
IParameterConvention convention)
{
options.DependencyInjectionBehavior = new ConventionDependencyInjectionBehavior(
options.DependencyInjectionBehavior, convention, options.Container);
}
private class ConventionDependencyInjectionBehavior : IDependencyInjectionBehavior
{
private IDependencyInjectionBehavior decorated;
private IParameterConvention convention;
private Container container;
public ConventionDependencyInjectionBehavior(
IDependencyInjectionBehavior decorated, IParameterConvention convention,
Container container)
{
this.decorated = decorated;
this.convention = convention;
this.container = container;
}
[DebuggerStepThrough]
public bool VerifyDependency(InjectionConsumerInfo dependency, out string errorMessage)
{
if (this.convention.CanResolve(dependency.Target))
{
errorMessage = null;
return true;
}
else
{
return this.decorated.VerifyDependency(dependency, out errorMessage);
}
}
[DebuggerStepThrough]
public InstanceProducer GetInstanceProducer(InjectionConsumerInfo dependency, bool throwOnFailure)
{
if (!this.convention.CanResolve(dependency.Target))
{
return this.decorated.GetInstanceProducer(dependency, throwOnFailure);
}
return InstanceProducer.FromExpression(
serviceType: dependency.Target.TargetType,
expression: this.convention.BuildExpression(dependency),
container: this.container);
}
}
}
// Example usage:
// new ConnectionStringsConvention(name => ConfigurationManager.ConnectionStrings[name]?.ConnectionString)
public class ConnectionStringsConvention : IParameterConvention
{
private const string ConnectionStringPostFix = "ConnectionString";
private readonly Func<string, string> connectionStringRetriever;
public ConnectionStringsConvention(Func<string, string> connectionStringRetriever)
{
this.connectionStringRetriever = connectionStringRetriever;
}
[DebuggerStepThrough]
public bool CanResolve(InjectionTargetInfo target)
{
bool resolvable =
target.TargetType == typeof(string) &&
target.Name.EndsWith(ConnectionStringPostFix) &&
target.Name.LastIndexOf(ConnectionStringPostFix) > 0;
if (resolvable)
{
this.VerifyConfigurationFile(target);
}
return resolvable;
}
[DebuggerStepThrough]
public Expression BuildExpression(InjectionConsumerInfo consumer)
{
string connectionString = this.GetConnectionString(consumer.Target);
return Expression.Constant(connectionString, typeof(string));
}
[DebuggerStepThrough]
private void VerifyConfigurationFile(InjectionTargetInfo target)
{
this.GetConnectionString(target);
}
[DebuggerStepThrough]
private string GetConnectionString(InjectionTargetInfo target)
{
string name = target.Name.Substring(0,
target.Name.LastIndexOf(ConnectionStringPostFix));
var connectionString = this.connectionStringRetriever(name);
if (connectionString is null)
{
throw new InvalidOperationException(
"No connection string with name '" + name + "' could be found in the " +
"application's configuration file.");
}
return connectionString;
}
}
// Example usage:
// new AppSettingsConvention(key => ConfigurationManager.AppSettings[key]);
public class AppSettingsConvention : IParameterConvention
{
private const string AppSettingsPostFix = "AppSetting";
private readonly Func<string, string> appSettingRetriever;
public AppSettingsConvention(Func<string, string> appSettingRetriever)
{
this.appSettingRetriever = appSettingRetriever;
}
[DebuggerStepThrough]
public bool CanResolve(InjectionTargetInfo target)
{
Type type = target.TargetType;
bool resolvable =
(type.IsValueType || type == typeof(string)) &&
target.Name.EndsWith(AppSettingsPostFix) &&
target.Name.LastIndexOf(AppSettingsPostFix) > 0;
if (resolvable)
{
this.VerifyConfigurationFile(target);
}
return resolvable;
}
[DebuggerStepThrough]
public Expression BuildExpression(InjectionConsumerInfo consumer)
{
object valueToInject = this.GetAppSettingValue(consumer.Target);
return Expression.Constant(valueToInject, consumer.Target.TargetType);
}
[DebuggerStepThrough]
private void VerifyConfigurationFile(InjectionTargetInfo target)
{
this.GetAppSettingValue(target);
}
[DebuggerStepThrough]
private object GetAppSettingValue(InjectionTargetInfo target)
{
string key = target.Name.Substring(0,
target.Name.LastIndexOf(AppSettingsPostFix));
string configurationValue = this.appSettingRetriever(key); // ConfigurationManager.AppSettings[key];
if (configurationValue != null)
{
System.ComponentModel.TypeConverter converter =
System.ComponentModel.TypeDescriptor.GetConverter(target.TargetType);
return converter.ConvertFromString(null,
CultureInfo.InvariantCulture, configurationValue);
}
throw new InvalidOperationException(
"No application setting with key '" + key + "' could be found in the " +
"application's configuration file.");
}
}
// Using optional parameters in constructor arguments is highly discouraged.
// This code is merely an example.
public class OptionalParameterConvention : IParameterConvention
{
private readonly IDependencyInjectionBehavior injectionBehavior;
public OptionalParameterConvention(IDependencyInjectionBehavior injectionBehavior)
{
this.injectionBehavior = injectionBehavior;
}
[DebuggerStepThrough]
public bool CanResolve(InjectionTargetInfo target) =>
target.Parameter != null && target.GetCustomAttributes(typeof(OptionalAttribute), true).Any();
[DebuggerStepThrough]
public Expression BuildExpression(InjectionConsumerInfo dependency) =>
this.GetProducer(dependency)?.BuildExpression() ?? GetDefault(dependency.Target.Parameter);
private InstanceProducer GetProducer(InjectionConsumerInfo dependency) =>
this.injectionBehavior.GetInstanceProducer(dependency, throwOnFailure: false);
private static ConstantExpression GetDefault(ParameterInfo parameter) =>
Expression.Constant(parameter.RawDefaultValue, parameter.ParameterType);
}
}