/
ConfigurationBuilderExtensions.cs
63 lines (59 loc) · 2.69 KB
/
ConfigurationBuilderExtensions.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
namespace Boilerplate.AspNetCore
{
using System;
using Microsoft.Extensions.Configuration;
/// <summary>
/// <see cref="IConfigurationBuilder"/> extension methods
/// </summary>
public static class ConfigurationBuilderExtensions
{
/// <summary>
/// Executes the specified action if the specified <paramref name="condition"/> is <c>true</c> which can be
/// used to conditionally add to the configuration pipeline.
/// </summary>
/// <param name="configurationBuilder">The configuration builder.</param>
/// <param name="condition">If set to <c>true</c> the action is executed.</param>
/// <param name="action">The action used to add to the request execution pipeline.</param>
/// <returns>The same configuration builder.</returns>
public static IConfigurationBuilder AddIf(
this IConfigurationBuilder configurationBuilder,
bool condition,
Func<IConfigurationBuilder, IConfigurationBuilder> action)
{
if (condition)
{
configurationBuilder = action(configurationBuilder);
}
return configurationBuilder;
}
/// <summary>
/// Executes the specified <paramref name="ifAction"/> if the specified <paramref name="condition"/> is
/// <c>true</c>, otherwise executes the <paramref name="elseAction"/>. This can be used to conditionally add to
/// the configuration pipeline.
/// </summary>
/// <param name="configurationBuilder">The configuration builder.</param>
/// <param name="condition">If set to <c>true</c> the <paramref name="ifAction"/> is executed, otherwise the
/// <paramref name="elseAction"/> is executed.</param>
/// <param name="ifAction">The action used to add to the configuration pipeline if the condition is
/// <c>true</c>.</param>
/// <param name="elseAction">The action used to add to the configuration pipeline if the condition is
/// <c>false</c>.</param>
/// <returns>The same configuration builder.</returns>
public static IConfigurationBuilder AddIfElse(
this IConfigurationBuilder configurationBuilder,
bool condition,
Func<IConfigurationBuilder, IConfigurationBuilder> ifAction,
Func<IConfigurationBuilder, IConfigurationBuilder> elseAction)
{
if (condition)
{
configurationBuilder = ifAction(configurationBuilder);
}
else
{
configurationBuilder = elseAction(configurationBuilder);
}
return configurationBuilder;
}
}
}