Skip to content

Commit 54d9cf3

Browse files
author
Matthias Gessinger
committed
Extract PolicyBuilder base interface
1 parent 219f5c7 commit 54d9cf3

File tree

4 files changed

+93
-57
lines changed

4 files changed

+93
-57
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
/// <summary>
6+
/// Defines the behavior of a policy builder which applies to a single API version.
7+
/// </summary>
8+
/// <typeparam name="TPolicy">The type of policy which is built by this builder.</typeparam>
9+
public interface IPolicyBuilder<TPolicy>
10+
{
11+
/// <summary>
12+
/// Gets the policy name.
13+
/// </summary>
14+
/// <value>The policy name, if any.</value>
15+
/// <remarks>The name is typically of an API.</remarks>
16+
string? Name { get; }
17+
18+
/// <summary>
19+
/// Gets the API version the policy is for.
20+
/// </summary>
21+
/// <value>The specific policy <see cref="ApiVersion">API version</see>, if any.</value>
22+
ApiVersion? ApiVersion { get; }
23+
24+
/// <summary>
25+
/// Configures the builder per the specified <paramref name="policy"/>.
26+
/// </summary>
27+
/// <param name="policy">The applied <typeparamref name="TPolicy">policy</typeparamref>.</param>
28+
void Per( TPolicy policy );
29+
30+
/// <summary>
31+
/// Builds and returns a policy.
32+
/// </summary>
33+
/// <returns>A new <typeparamref name="TPolicy">policy</typeparamref>.</returns>
34+
TPolicy Build();
35+
}

src/Abstractions/src/Asp.Versioning.Abstractions/ISunsetPolicyBuilder.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,14 @@ namespace Asp.Versioning;
55
/// <summary>
66
/// Defines the behavior of a sunset policy builder.
77
/// </summary>
8-
public interface ISunsetPolicyBuilder
8+
public interface ISunsetPolicyBuilder : IPolicyBuilder<SunsetPolicy>
99
{
1010
/// <summary>
11-
/// Gets the policy name.
12-
/// </summary>
13-
/// <value>The policy name, if any.</value>
14-
/// <remarks>The name is typically of an API.</remarks>
15-
string? Name { get; }
16-
17-
/// <summary>
18-
/// Gets the API version the policy is for.
19-
/// </summary>
20-
/// <value>The specific policy <see cref="ApiVersion">API version</see>, if any.</value>
21-
ApiVersion? ApiVersion { get; }
22-
23-
/// <summary>
24-
/// Applies a sunset policy per the specified policy.
11+
/// Creates and returns a new link builder.
2512
/// </summary>
26-
/// <param name="policy">The applied <see cref="SunsetPolicy">sunset policy</see>.</param>
27-
void Per( SunsetPolicy policy );
13+
/// <param name="linkTarget">The link target URL.</param>
14+
/// <returns>A new <see cref="ILinkBuilder">link builder</see>.</returns>
15+
ILinkBuilder Link( Uri linkTarget );
2816

2917
/// <summary>
3018
/// Indicates when a sunset policy is applied.
@@ -33,17 +21,4 @@ public interface ISunsetPolicyBuilder
3321
/// sunset policy is applied.</param>
3422
/// <returns>The current <see cref="ISunsetPolicyBuilder">sunset policy builder</see>.</returns>
3523
ISunsetPolicyBuilder Effective( DateTimeOffset sunsetDate );
36-
37-
/// <summary>
38-
/// Creates and returns a new link builder.
39-
/// </summary>
40-
/// <param name="linkTarget">The link target URL.</param>
41-
/// <returns>A new <see cref="ILinkBuilder">link builder</see>.</returns>
42-
ILinkBuilder Link( Uri linkTarget );
43-
44-
/// <summary>
45-
/// Builds and returns a sunset policy.
46-
/// </summary>
47-
/// <returns>A new <see cref="SunsetPolicy">sunset policy</see>.</returns>
48-
SunsetPolicy Build();
4924
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
using System.Globalization;
6+
7+
/// <summary>
8+
/// Represents the default policy builder.
9+
/// </summary>
10+
/// <typeparam name="TPolicy">The type of policy.</typeparam>
11+
public abstract class PolicyBuilder<TPolicy> : IPolicyBuilder<TPolicy>
12+
{
13+
/// <summary>
14+
/// Gets a pre-built policy.
15+
/// </summary>
16+
/// <value>The pre-built policy, if it exists.</value>
17+
protected TPolicy? Policy { get; private set; }
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="PolicyBuilder{T}"/> class.
21+
/// </summary>
22+
/// <param name="name">The name of the API the policy is for.</param>
23+
/// <param name="apiVersion">The <see cref="ApiVersion">API version</see> the policy is for.</param>
24+
public PolicyBuilder( string? name, ApiVersion? apiVersion )
25+
{
26+
if ( string.IsNullOrEmpty( name ) && apiVersion == null )
27+
{
28+
var message = string.Format( CultureInfo.CurrentCulture, Format.InvalidPolicyKey, nameof( name ), nameof( apiVersion ) );
29+
throw new System.ArgumentException( message );
30+
}
31+
32+
Name = name;
33+
ApiVersion = apiVersion;
34+
}
35+
36+
/// <inheritdoc />
37+
public string? Name { get; }
38+
39+
/// <inheritdoc />
40+
public ApiVersion? ApiVersion { get; }
41+
42+
/// <inheritdoc />
43+
public virtual void Per( TPolicy policy ) =>
44+
Policy = policy ?? throw new System.ArgumentNullException( nameof( policy ) );
45+
46+
/// <inheritdoc />
47+
public abstract TPolicy Build();
48+
}

src/Common/src/Common/SunsetPolicyBuilder.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace Asp.Versioning;
44

5-
using System.Globalization;
6-
75
/// <summary>
86
/// Represents the default sunset policy builder.
97
/// </summary>
10-
public class SunsetPolicyBuilder : ISunsetPolicyBuilder
8+
public class SunsetPolicyBuilder : PolicyBuilder<SunsetPolicy>, ISunsetPolicyBuilder
119
{
12-
private SunsetPolicy? sunsetPolicy;
1310
private DateTimeOffset? date;
1411
private SunsetLinkBuilder? linkBuilder;
1512
private Dictionary<Uri, SunsetLinkBuilder>? linkBuilders;
@@ -20,26 +17,7 @@ public class SunsetPolicyBuilder : ISunsetPolicyBuilder
2017
/// <param name="name">The name of the API the policy is for.</param>
2118
/// <param name="apiVersion">The <see cref="ApiVersion">API version</see> the policy is for.</param>
2219
public SunsetPolicyBuilder( string? name, ApiVersion? apiVersion )
23-
{
24-
if ( string.IsNullOrEmpty( name ) && apiVersion == null )
25-
{
26-
var message = string.Format( CultureInfo.CurrentCulture, Format.InvalidPolicyKey, nameof( name ), nameof( apiVersion ) );
27-
throw new System.ArgumentException( message );
28-
}
29-
30-
Name = name;
31-
ApiVersion = apiVersion;
32-
}
33-
34-
/// <inheritdoc />
35-
public string? Name { get; }
36-
37-
/// <inheritdoc />
38-
public ApiVersion? ApiVersion { get; }
39-
40-
/// <inheritdoc />
41-
public virtual void Per( SunsetPolicy policy ) =>
42-
sunsetPolicy = policy ?? throw new System.ArgumentNullException( nameof( policy ) );
20+
: base( name, apiVersion ) { }
4321

4422
/// <inheritdoc />
4523
public virtual ISunsetPolicyBuilder Effective( DateTimeOffset sunsetDate )
@@ -78,11 +56,11 @@ public virtual ILinkBuilder Link( Uri linkTarget )
7856
}
7957

8058
/// <inheritdoc />
81-
public virtual SunsetPolicy Build()
59+
public override SunsetPolicy Build()
8260
{
83-
if ( sunsetPolicy is not null )
61+
if ( Policy is not null )
8462
{
85-
return sunsetPolicy;
63+
return Policy;
8664
}
8765

8866
SunsetPolicy policy = date is null ? new() : new( date.Value );

0 commit comments

Comments
 (0)