-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceCollectionExtensions.cs
74 lines (61 loc) · 2.46 KB
/
ServiceCollectionExtensions.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
using Arex388.AspNetCore;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
namespace Microsoft.Extensions.DependencyInjection {
public static class ServiceCollectionExtensions {
public static IServiceCollection AddArex388(
this IServiceCollection services,
Action<Arex388Options> configurer) {
if (services is null) {
throw new ArgumentNullException(nameof(services));
}
if (configurer is null) {
throw new ArgumentNullException(nameof(configurer));
}
var options = new Arex388Options();
configurer(options);
ConfigureFeatures(services, options);
ConfigureIdentityProvider(services, options);
ConfigureSimpleSlugifyParameterTransformer(services, options);
return services;
}
private static void ConfigureFeatures(
IServiceCollection services,
Arex388Options options) {
if (!options.UseFeatures) {
return;
}
services.Configure<RazorViewEngineOptions>(
o => {
o.ViewLocationExpanders.Clear();
o.ViewLocationExpanders.Add(new FeaturesViewLocationExpander());
});
}
private static void ConfigureIdentityProvider(
IServiceCollection services,
Arex388Options options) {
if (!options.UseIdentityProvider) {
return;
}
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.TryAddScoped<IdentityProvider>();
}
private static void ConfigureSimpleSlugifyParameterTransformer(
IServiceCollection services,
Arex388Options options) {
if (!options.UseSimpleSlugifyParameterTransformer) {
return;
}
services.Configure<RouteOptions>(
o => {
if (o.ConstraintMap.ContainsKey("slugify")) {
throw new InvalidOperationException("An IOutboundParameterTransformer with a key of 'slugify' is already registered.");
}
o.ConstraintMap["slugify"] = typeof(SimpleSlugifyParameterTransformer);
});
}
}
}