This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
ConfigureCompatibilityOptions.cs
105 lines (92 loc) · 3.88 KB
/
ConfigureCompatibilityOptions.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
/// <summary>
/// A base class for infrastructure that implements ASP.NET Core MVC's support for
/// <see cref="CompatibilityVersion"/>. This is framework infrastructure and should not be used
/// by application code.
/// </summary>
/// <typeparam name="TOptions"></typeparam>
public abstract class ConfigureCompatibilityOptions<TOptions> : IPostConfigureOptions<TOptions>
where TOptions : class, IEnumerable<ICompatibilitySwitch>
{
private readonly ILogger _logger;
/// <summary>
/// Creates a new <see cref="ConfigureCompatibilityOptions{TOptions}"/>.
/// </summary>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="compatibilityOptions">The <see cref="IOptions{MvcCompatibilityOptions}"/>.</param>
protected ConfigureCompatibilityOptions(
ILoggerFactory loggerFactory,
IOptions<MvcCompatibilityOptions> compatibilityOptions)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
Version = compatibilityOptions.Value.CompatibilityVersion;
_logger = loggerFactory.CreateLogger<TOptions>();
}
/// <summary>
/// Gets the default values of compatibility switches associated with the applications configured
/// <see cref="CompatibilityVersion"/>.
/// </summary>
protected abstract IReadOnlyDictionary<string, object> DefaultValues { get; }
/// <summary>
/// Gets the <see cref="CompatibilityVersion"/> configured for the application.
/// </summary>
protected CompatibilityVersion Version { get; }
/// <inheritdoc />
public virtual void PostConfigure(string name, TOptions options)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
// Evaluate DefaultValues onces so subclasses don't have to cache.
var defaultValues = DefaultValues;
foreach (var @switch in options)
{
ConfigureSwitch(@switch, defaultValues);
}
}
private void ConfigureSwitch(ICompatibilitySwitch @switch, IReadOnlyDictionary<string, object> defaultValues)
{
if (@switch.IsValueSet)
{
_logger.LogDebug(
"Compatibility switch {SwitchName} in type {OptionsType} is using explicitly configured value {Value}",
@switch.Name,
typeof(TOptions).Name,
@switch.Value);
return;
}
if (!defaultValues.TryGetValue(@switch.Name, out var value))
{
_logger.LogDebug(
"Compatibility switch {SwitchName} in type {OptionsType} is using default value {Value}",
@switch.Name,
typeof(TOptions).Name,
@switch.Value,
Version);
return;
}
@switch.Value = value;
_logger.LogDebug(
"Compatibility switch {SwitchName} in type {OptionsType} is using compatibility value {Value} for version {Version}",
@switch.Name,
typeof(TOptions).Name,
@switch.Value,
Version);
}
}
}