Skip to content

Commit 500a8e0

Browse files
committed
Add IWebHostBuilder UseIf and UseIfElse extension methods with condition as a Func<IWebHostBuilder, bool>
1 parent 54f44b9 commit 500a8e0

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

Source/Boxed.AspNetCore/Boxed.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<PropertyGroup Label="Package">
12-
<VersionPrefix>3.0.0</VersionPrefix>
12+
<VersionPrefix>3.1.0</VersionPrefix>
1313
<Authors>Muhammad Rehan Saeed (RehanSaeed.com)</Authors>
1414
<Product>ASP.NET Core Framework Boxed</Product>
1515
<Description>Provides ASP.NET Core middleware, MVC filters, extension methods and helper code for an ASP.NET Core project.</Description>

Source/Boxed.AspNetCore/WebHostBuilderExtensions.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ public static IWebHostBuilder UseIf(
3939
return hostBuilder;
4040
}
4141

42+
/// <summary>
43+
/// Executes the specified action if the specified <paramref name="condition"/> is <c>true</c> which can be
44+
/// used to conditionally add to the host builder.
45+
/// </summary>
46+
/// <param name="hostBuilder">The host builder.</param>
47+
/// <param name="condition">If <c>true</c> is returned the action is executed.</param>
48+
/// <param name="action">The action used to add to the host builder.</param>
49+
/// <returns>The same host builder.</returns>
50+
public static IWebHostBuilder UseIf(
51+
this IWebHostBuilder hostBuilder,
52+
Func<IWebHostBuilder, bool> condition,
53+
Func<IWebHostBuilder, IWebHostBuilder> action)
54+
{
55+
if (hostBuilder == null)
56+
{
57+
throw new ArgumentNullException(nameof(hostBuilder));
58+
}
59+
60+
if (action == null)
61+
{
62+
throw new ArgumentNullException(nameof(action));
63+
}
64+
65+
if (condition(hostBuilder))
66+
{
67+
hostBuilder = action(hostBuilder);
68+
}
69+
70+
return hostBuilder;
71+
}
72+
4273
/// <summary>
4374
/// Executes the specified <paramref name="ifAction"/> if the specified <paramref name="condition"/> is
4475
/// <c>true</c>, otherwise executes the <paramref name="elseAction"/>. This can be used to conditionally add to
@@ -82,5 +113,49 @@ public static IWebHostBuilder UseIfElse(
82113

83114
return hostBuilder;
84115
}
116+
117+
/// <summary>
118+
/// Executes the specified <paramref name="ifAction"/> if the specified <paramref name="condition"/> is
119+
/// <c>true</c>, otherwise executes the <paramref name="elseAction"/>. This can be used to conditionally add to
120+
/// the host builder.
121+
/// </summary>
122+
/// <param name="hostBuilder">The host builder.</param>
123+
/// <param name="condition">If <c>true</c> is returned the <paramref name="ifAction"/> is executed, otherwise the
124+
/// <paramref name="elseAction"/> is executed.</param>
125+
/// <param name="ifAction">The action used to add to the host builder if the condition is <c>true</c>.</param>
126+
/// <param name="elseAction">The action used to add to the host builder if the condition is <c>false</c>.</param>
127+
/// <returns>The same host builder.</returns>
128+
public static IWebHostBuilder UseIfElse(
129+
this IWebHostBuilder hostBuilder,
130+
Func<IWebHostBuilder, bool> condition,
131+
Func<IWebHostBuilder, IWebHostBuilder> ifAction,
132+
Func<IWebHostBuilder, IWebHostBuilder> elseAction)
133+
{
134+
if (hostBuilder == null)
135+
{
136+
throw new ArgumentNullException(nameof(hostBuilder));
137+
}
138+
139+
if (ifAction == null)
140+
{
141+
throw new ArgumentNullException(nameof(ifAction));
142+
}
143+
144+
if (elseAction == null)
145+
{
146+
throw new ArgumentNullException(nameof(elseAction));
147+
}
148+
149+
if (condition(hostBuilder))
150+
{
151+
hostBuilder = ifAction(hostBuilder);
152+
}
153+
else
154+
{
155+
hostBuilder = elseAction(hostBuilder);
156+
}
157+
158+
return hostBuilder;
159+
}
85160
}
86161
}

0 commit comments

Comments
 (0)