Skip to content

Commit 765dcf1

Browse files
author
Matthias Gessinger
committed
Define new interfaces for policies
ISunsetPolicyBuilder and IDeprecationPolicyBuilder contained duplicated functionality. This was extracted into the new interfaces IPolicyWithLink and IPolicyWithEffectiveDate and corresponding extension methods. To avoid naming collisions, the `Effective` method was renamed to `SetEffectiveDate`.
1 parent 064c5c9 commit 765dcf1

File tree

11 files changed

+93
-118
lines changed

11 files changed

+93
-118
lines changed

src/Abstractions/src/Asp.Versioning.Abstractions/IDeprecationPolicyBuilder.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,4 @@ namespace Asp.Versioning;
55
/// <summary>
66
/// Defines the behavior of a deprecation policy builder.
77
/// </summary>
8-
public interface IDeprecationPolicyBuilder : IPolicyBuilder<DeprecationPolicy>
9-
{
10-
/// <summary>
11-
/// Creates and returns a new link builder.
12-
/// </summary>
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 );
16-
17-
/// <summary>
18-
/// Indicates when a deprecation policy is applied.
19-
/// </summary>
20-
/// <param name="deprecationDate">The <see cref="DateTimeOffset">date and time</see> when a
21-
/// deprecation policy is applied.</param>
22-
/// <returns>The current <see cref="IDeprecationPolicyBuilder">deprecation policy builder</see>.</returns>
23-
IDeprecationPolicyBuilder Effective( DateTimeOffset deprecationDate );
24-
}
8+
public interface IDeprecationPolicyBuilder : IPolicyBuilder<DeprecationPolicy>, IPolicyWithLink, IPolicyWithEffectiveDate { }

src/Abstractions/src/Asp.Versioning.Abstractions/IDeprecationPolicyBuilderExtensions.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
/// <summary>
6+
/// Provides extension methods for the <see cref="IPolicyBuilder{T}"/> interface.
7+
/// </summary>
8+
public static class IPolicyBuilderExtensions
9+
{
10+
/// <summary>
11+
/// Creates and returns a new link builder.
12+
/// </summary>
13+
/// <param name="builder">The extended <see cref="IPolicyBuilder{T}">policy builder</see>.</param>
14+
/// <param name="linkTarget">The link target URL.</param>
15+
/// <returns>A new <see cref="ILinkBuilder">link builder</see>.</returns>
16+
public static ILinkBuilder Link( this IPolicyWithLink builder, string linkTarget )
17+
{
18+
ArgumentNullException.ThrowIfNull( builder );
19+
return builder.Link( new Uri( linkTarget, UriKind.RelativeOrAbsolute ) );
20+
}
21+
22+
/// <summary>
23+
/// Indicates when a policy is applied.
24+
/// </summary>
25+
/// <typeparam name="TBuilder">The type of <see cref="IPolicyBuilder{T}">policy builder</see>.</typeparam>
26+
/// <param name="builder">The extended <see cref="IPolicyBuilder{T}">policy builder</see>.</param>
27+
/// <param name="effectiveDate">The time when the policy is applied.</param>
28+
/// <returns>The current <see cref="IPolicyBuilder{T}">policy builder</see>.</returns>
29+
public static TBuilder Effective<TBuilder>( this TBuilder builder, DateTimeOffset effectiveDate )
30+
where TBuilder : notnull, IPolicyWithEffectiveDate
31+
{
32+
ArgumentNullException.ThrowIfNull( builder );
33+
builder.SetEffectiveDate( effectiveDate );
34+
return builder;
35+
}
36+
37+
/// <summary>
38+
/// Indicates when a policy is applied.
39+
/// </summary>
40+
/// <typeparam name="TBuilder">The type of <see cref="IPolicyBuilder{T}">policy builder</see>.</typeparam>
41+
/// <param name="builder">The extended <see cref="IPolicyBuilder{T}">policy builder</see>.</param>
42+
/// <param name="year">The year when the policy is applied.</param>
43+
/// <param name="month">The month when the policy is applied.</param>
44+
/// <param name="day">The day when the policy is applied.</param>
45+
/// <returns>The current <see cref="IPolicyBuilder{T}">policy builder</see>.</returns>
46+
public static TBuilder Effective<TBuilder>( this TBuilder builder, int year, int month, int day )
47+
where TBuilder : notnull, IPolicyWithEffectiveDate
48+
{
49+
ArgumentNullException.ThrowIfNull( builder );
50+
return builder.Effective( new DateTimeOffset( new DateTime( year, month, day ) ) );
51+
}
52+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
/// <summary>
6+
/// A policy which can be configured to only be effective after a particular date.
7+
/// </summary>
8+
public interface IPolicyWithEffectiveDate
9+
{
10+
/// <summary>
11+
/// Indicates when a policy is applied.
12+
/// </summary>
13+
/// <param name="effectiveDate">
14+
/// The <see cref="DateTimeOffset">date and time</see> when a policy is applied.
15+
/// </param>
16+
void SetEffectiveDate( DateTimeOffset effectiveDate );
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
/// <summary>
6+
/// Defines a policy which can (optionally) expose a link to more information.
7+
/// </summary>
8+
public interface IPolicyWithLink
9+
{
10+
/// <summary>
11+
/// Creates and returns a new link builder.
12+
/// </summary>
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 );
16+
}

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,4 @@ namespace Asp.Versioning;
55
/// <summary>
66
/// Defines the behavior of a sunset policy builder.
77
/// </summary>
8-
public interface ISunsetPolicyBuilder : IPolicyBuilder<SunsetPolicy>
9-
{
10-
/// <summary>
11-
/// Creates and returns a new link builder.
12-
/// </summary>
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 );
16-
17-
/// <summary>
18-
/// Indicates when a sunset policy is applied.
19-
/// </summary>
20-
/// <param name="sunsetDate">The <see cref="DateTimeOffset">date and time</see> when a
21-
/// sunset policy is applied.</param>
22-
/// <returns>The current <see cref="ISunsetPolicyBuilder">sunset policy builder</see>.</returns>
23-
ISunsetPolicyBuilder Effective( DateTimeOffset sunsetDate );
24-
}
8+
public interface ISunsetPolicyBuilder : IPolicyBuilder<SunsetPolicy>, IPolicyWithLink, IPolicyWithEffectiveDate { }

src/Abstractions/src/Asp.Versioning.Abstractions/ISunsetPolicyBuilderExtensions.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Abstractions/test/Asp.Versioning.Abstractions.Tests/ISunsetPolicyManagerExtensionsTest.cs renamed to src/Abstractions/test/Asp.Versioning.Abstractions.Tests/IPolicyManagerExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Asp.Versioning;
44

5-
public class ISunsetPolicyManagerExtensionsTest
5+
public class IPolicyManagerExtensionsTest
66
{
77
[Fact]
88
public void try_get_policy_should_get_global_policy_by_version()

src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/ODataApiDescriptionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,4 @@ public int GetHashCode( [DisallowNull] ApiDescription obj )
673673
return hash.ToHashCode();
674674
}
675675
}
676-
}
676+
}

src/Common/src/Common/DeprecationPolicyBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ public DeprecationPolicyBuilder( string? name, ApiVersion? apiVersion )
2020
: base( name, apiVersion ) { }
2121

2222
/// <inheritdoc />
23-
public virtual IDeprecationPolicyBuilder Effective( DateTimeOffset deprecationDate )
23+
public virtual void SetEffectiveDate( DateTimeOffset effectiveDate )
2424
{
25-
date = deprecationDate;
26-
return this;
25+
date = effectiveDate;
2726
}
2827

2928
/// <inheritdoc />

0 commit comments

Comments
 (0)