-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathJsEngineSwitcherServiceCollectionExtensions.cs
More file actions
194 lines (163 loc) · 6.7 KB
/
JsEngineSwitcherServiceCollectionExtensions.cs
File metadata and controls
194 lines (163 loc) · 6.7 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
using System;
using Microsoft.Extensions.DependencyInjection;
using JavaScriptEngineSwitcher.Core;
namespace JavaScriptEngineSwitcher.Extensions.MsDependencyInjection
{
/// <summary>
/// Extension methods for adding the JS engine switcher in an <see cref="IServiceCollection"/>
/// </summary>
public static class JsEngineSwitcherServiceCollectionExtensions
{
/// <summary>
/// Adds a default instance of the JS engine switcher to the <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <returns>Instance of the <see cref="JsEngineFactoryCollection"/></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
var options = new JsEngineSwitcherOptions();
IJsEngineSwitcher engineSwitcher = CreateJsEngineSwitcher(options);
ApplyOptionsToJsEngineSwitcher(engineSwitcher, options);
RegisterJsEngineSwitcher(services, engineSwitcher, options);
return engineSwitcher.EngineFactories;
}
/// <summary>
/// Adds a specified instance of the JS engine switcher to the <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="engineSwitcher">Instance of the JS engine switcher</param>
/// <returns>Instance of the <see cref="JsEngineFactoryCollection"/></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services,
IJsEngineSwitcher engineSwitcher)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
if (engineSwitcher is null)
{
throw new ArgumentNullException(nameof(engineSwitcher));
}
var options = new JsEngineSwitcherOptions();
IJsEngineSwitcher currentEngineSwitcher = GetJsEngineSwitcher(engineSwitcher, options);
ApplyOptionsToJsEngineSwitcher(currentEngineSwitcher, options);
RegisterJsEngineSwitcher(services, currentEngineSwitcher, options);
return currentEngineSwitcher.EngineFactories;
}
/// <summary>
/// Adds a default instance of the JS engine switcher to the <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="configure">The <see cref="JsEngineSwitcherOptions"/> which need to be configured</param>
/// <returns>Instance of the <see cref="JsEngineFactoryCollection"/></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services,
Action<JsEngineSwitcherOptions> configure)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}
var options = new JsEngineSwitcherOptions();
configure(options);
IJsEngineSwitcher engineSwitcher = CreateJsEngineSwitcher(options);
ApplyOptionsToJsEngineSwitcher(engineSwitcher, options);
RegisterJsEngineSwitcher(services, engineSwitcher, options);
return engineSwitcher.EngineFactories;
}
/// <summary>
/// Adds a specified instance of the JS engine switcher to <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="engineSwitcher">Instance of the JS engine switcher</param>
/// <param name="configure">The <see cref="JsEngineSwitcherOptions"/> which need to be configured</param>
/// <returns>Instance of the <see cref="JsEngineFactoryCollection"/></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services,
IJsEngineSwitcher engineSwitcher, Action<JsEngineSwitcherOptions> configure)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
if (engineSwitcher is null)
{
throw new ArgumentNullException(nameof(engineSwitcher));
}
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}
var options = new JsEngineSwitcherOptions();
configure(options);
IJsEngineSwitcher currentEngineSwitcher = GetJsEngineSwitcher(engineSwitcher, options);
ApplyOptionsToJsEngineSwitcher(currentEngineSwitcher, options);
RegisterJsEngineSwitcher(services, currentEngineSwitcher, options);
return currentEngineSwitcher.EngineFactories;
}
#region Helper methods
/// <summary>
/// Creates an instance of the JS engine switcher
/// </summary>
/// <param name="options">Options of the JS engine switcher</param>
/// <returns>Instance of the JS engine switcher</returns>
private static IJsEngineSwitcher CreateJsEngineSwitcher(JsEngineSwitcherOptions options)
{
IJsEngineSwitcher engineSwitcher = options.AllowCurrentProperty ?
JsEngineSwitcher.Current
:
new JsEngineSwitcher()
;
return engineSwitcher;
}
/// <summary>
/// Gets a instance of the JS engine switcher
/// </summary>
/// <param name="engineSwitcher">Instance of the JS engine switcher</param>
/// <param name="options">Options of the JS engine switcher</param>
/// <returns>Current instance of the JS engine switcher</returns>
private static IJsEngineSwitcher GetJsEngineSwitcher(IJsEngineSwitcher engineSwitcher, JsEngineSwitcherOptions options)
{
IJsEngineSwitcher currentEngineSwitcher = options.AllowCurrentProperty ?
JsEngineSwitcher.Current
:
engineSwitcher
;
return currentEngineSwitcher;
}
/// <summary>
/// Applies a options to the JS engine switcher
/// </summary>
/// <param name="engineSwitcher">Instance of the JS engine switcher</param>
/// <param name="options">Options of the JS engine switcher</param>
private static void ApplyOptionsToJsEngineSwitcher(IJsEngineSwitcher engineSwitcher, JsEngineSwitcherOptions options)
{
JsEngineSwitcher.AllowCurrentProperty = options.AllowCurrentProperty;
engineSwitcher.DefaultEngineName = options.DefaultEngineName;
}
/// <summary>
/// Registers a instance of the JS engine switcher
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="engineSwitcher">Instance of the JS engine switcher</param>
/// <param name="options">Options of the JS engine switcher</param>
private static void RegisterJsEngineSwitcher(IServiceCollection services, IJsEngineSwitcher engineSwitcher,
JsEngineSwitcherOptions options)
{
// Register the current instance of JS engine switcher as a service
services.AddSingleton(engineSwitcher);
// Set the current instance of JS engine switcher
if (options.AllowCurrentProperty)
{
JsEngineSwitcher.Current = engineSwitcher;
}
}
#endregion
}
}