Skip to content

Commit eb0367d

Browse files
Matthias Gessingercommonsensesoftware
authored andcommitted
Pass relation type to LinkList to avoid subclassing
1 parent 76be97d commit eb0367d

6 files changed

Lines changed: 20 additions & 44 deletions

File tree

src/Abstractions/src/Asp.Versioning.Abstractions/DeprecationPolicy.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DeprecationPolicy
1414
/// </summary>
1515
public DeprecationPolicy()
1616
{
17-
links = new DeprecationLinkList();
17+
links = new LinkList( "deprecation" );
1818
}
1919

2020
/// <summary>
@@ -63,15 +63,4 @@ public DeprecationPolicy( LinkHeaderValue link )
6363
/// links might be provided for different languages or different formats such as a HTML page
6464
/// or a JSON file.</remarks>
6565
public IList<LinkHeaderValue> Links => links;
66-
67-
internal sealed class DeprecationLinkList : LinkList
68-
{
69-
protected override void EnsureRelationType( LinkHeaderValue item )
70-
{
71-
if ( !item.RelationType.Equals( "deprecation", StringComparison.OrdinalIgnoreCase ) )
72-
{
73-
throw new ArgumentException( SR.InvalidDeprecationRelationType, nameof( item ) );
74-
}
75-
}
76-
}
7766
}

src/Abstractions/src/Asp.Versioning.Abstractions/Format.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ internal static class Format
1111
#if NETSTANDARD
1212
internal static readonly string ApiVersionBadStatus = SR.ApiVersionBadStatus;
1313
internal static readonly string ApiVersionBadGroupVersion = SR.ApiVersionBadGroupVersion;
14+
internal static readonly string InvalidRelationType = SR.InvalidRelationType;
1415
#else
1516
internal static readonly CompositeFormat ApiVersionBadStatus = CompositeFormat.Parse( SR.ApiVersionBadStatus );
1617
internal static readonly CompositeFormat ApiVersionBadGroupVersion = CompositeFormat.Parse( SR.ApiVersionBadGroupVersion );
18+
internal static readonly CompositeFormat InvalidRelationType = CompositeFormat.Parse( SR.InvalidRelationType );
1719
#endif
1820
}

src/Abstractions/src/Asp.Versioning.Abstractions/LinkList.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace Asp.Versioning;
44

55
using System.Collections.ObjectModel;
6+
using System.Globalization;
67

7-
internal abstract class LinkList : Collection<LinkHeaderValue>
8+
internal sealed class LinkList( string relationType ) : Collection<LinkHeaderValue>
89
{
9-
public LinkList() { }
10+
private readonly string relationType = relationType;
1011

1112
protected override void InsertItem( int index, LinkHeaderValue item )
1213
{
@@ -20,5 +21,12 @@ protected override void SetItem( int index, LinkHeaderValue item )
2021
base.SetItem( index, item );
2122
}
2223

23-
protected abstract void EnsureRelationType( LinkHeaderValue item );
24+
private void EnsureRelationType( LinkHeaderValue item )
25+
{
26+
if ( !item.RelationType.Equals( relationType, StringComparison.OrdinalIgnoreCase ) )
27+
{
28+
var message = string.Format( CultureInfo.CurrentCulture, Format.InvalidRelationType, relationType );
29+
throw new ArgumentException( message, nameof( item ) );
30+
}
31+
}
2432
}

src/Abstractions/src/Asp.Versioning.Abstractions/SR.Designer.cs

Lines changed: 3 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Abstractions/src/Asp.Versioning.Abstractions/SR.resx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@
141141
<data name="InvalidOrMalformedHeader" xml:space="preserve">
142142
<value>The header contains invalid or missing values.</value>
143143
</data>
144-
<data name="InvalidSunsetRelationType" xml:space="preserve">
145-
<value>The relation type for a sunset policy link must be "sunset".</value>
146-
</data>
147-
<data name="InvalidDeprecationRelationType" xml:space="preserve">
148-
<value>The relation type for a deprecation policy link must be "deprecation".</value>
144+
<data name="InvalidRelationType" xml:space="preserve">
145+
<value>The relation type for a {0} policy link must be "{0}".</value>
149146
</data>
150147
</root>

src/Abstractions/src/Asp.Versioning.Abstractions/SunsetPolicy.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class SunsetPolicy
1414
/// </summary>
1515
public SunsetPolicy()
1616
{
17-
links = new SunsetLinkList();
17+
links = new LinkList( "sunset" );
1818
}
1919

2020
/// <summary>
@@ -63,15 +63,4 @@ public SunsetPolicy( LinkHeaderValue link )
6363
/// links might be provided for different languages or different formats such as a HTML page
6464
/// or a JSON file.</remarks>
6565
public IList<LinkHeaderValue> Links => links;
66-
67-
internal sealed class SunsetLinkList : LinkList
68-
{
69-
protected override void EnsureRelationType( LinkHeaderValue item )
70-
{
71-
if ( !item.RelationType.Equals( "sunset", StringComparison.OrdinalIgnoreCase ) )
72-
{
73-
throw new ArgumentException( SR.InvalidSunsetRelationType, nameof( item ) );
74-
}
75-
}
76-
}
7766
}

0 commit comments

Comments
 (0)