-
-
Notifications
You must be signed in to change notification settings - Fork 321
/
EndpointFactory.cs
60 lines (52 loc) · 1.91 KB
/
EndpointFactory.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
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace MigrationTools.Endpoints
{
class EndpointFactory : IEndpointFactory
{
private readonly ILogger _logger;
private readonly IServiceProvider _services;
private readonly IOptionsMonitor<EndpointFactoryOptions> _optionsMonitor;
public EndpointFactory(
IServiceProvider services,
ILogger<EndpointFactory> logger,
IOptionsMonitor<EndpointFactoryOptions> optionsMonitor)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
if (optionsMonitor == null)
{
throw new ArgumentNullException(nameof(optionsMonitor));
}
_services = services;
_optionsMonitor = optionsMonitor;
_logger = logger;
}
public IEndpoint CreateEndpoint(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
_logger.LogInformation("Creating endpoint with name {EndpointName}", name);
EndpointFactoryOptions options = _optionsMonitor.Get(name);
if (options.EndpointFuncs.Count == 0)
{
_logger.LogDebug("Endpoint count: {0}", _optionsMonitor.CurrentValue.EndpointFuncs.Count);
throw new InvalidOperationException($"There is no endpoint named [{name}]");
}
else if (options.EndpointFuncs.Count > 1)
{
throw new InvalidOperationException($"There duplicate endpoints with name [{name}]");
}
return options.EndpointFuncs[0](_services);
}
}
}