Skip to content

Commit 357658e

Browse files
Matthias Gessingercommonsensesoftware
authored andcommitted
Don't initialize LinkList internally unless necessary
1 parent eb0367d commit 357658e

2 files changed

Lines changed: 50 additions & 65 deletions

File tree

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

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,52 @@ namespace Asp.Versioning;
77
/// </summary>
88
public class DeprecationPolicy
99
{
10-
private readonly LinkList links;
10+
private LinkList? links;
11+
12+
/// <summary>
13+
/// Gets a read-only list of links that provide information about the deprecation policy.
14+
/// </summary>
15+
/// <value>A read-only list of HTTP links.</value>
16+
/// <remarks>If a link is provided, generally only one link is necessary; however, additional
17+
/// links might be provided for different languages or different formats such as a HTML page
18+
/// or a JSON file.</remarks>
19+
public IList<LinkHeaderValue> Links => links ??= new( "deprecation" );
20+
21+
/// <summary>
22+
/// Gets a value indicating whether the deprecation policy has any associated links.
23+
/// </summary>
24+
/// <value>True if the deprecation policy has associated links; otherwise, false.</value>
25+
public bool HasLinks => links is not null && links.Count > 0;
26+
27+
/// <summary>
28+
/// Gets the date and time when the API version will be deprecated.
29+
/// </summary>
30+
/// <value>The date and time when the API version will be deprecated, if any.</value>
31+
public DateTimeOffset? Date { get; }
1132

1233
/// <summary>
1334
/// Initializes a new instance of the <see cref="DeprecationPolicy"/> class.
1435
/// </summary>
15-
public DeprecationPolicy()
16-
{
17-
links = new LinkList( "deprecation" );
18-
}
36+
public DeprecationPolicy() { }
1937

2038
/// <summary>
2139
/// Initializes a new instance of the <see cref="DeprecationPolicy"/> class.
2240
/// </summary>
2341
/// <param name="date">The date and time when the API version will be deprecated.</param>
2442
/// <param name="link">The optional link which provides information about the deprecation policy.</param>
2543
public DeprecationPolicy( DateTimeOffset date, LinkHeaderValue? link = default )
26-
: this()
2744
{
2845
Date = date;
2946

3047
if ( link is not null )
3148
{
32-
links.Add( link );
49+
Links.Add( link );
3350
}
3451
}
3552

3653
/// <summary>
3754
/// Initializes a new instance of the <see cref="DeprecationPolicy"/> class.
3855
/// </summary>
3956
/// <param name="link">The link which provides information about the deprecation policy.</param>
40-
public DeprecationPolicy( LinkHeaderValue link )
41-
: this()
42-
{
43-
links.Add( link );
44-
}
45-
46-
/// <summary>
47-
/// Gets the date and time when the API version will be deprecated.
48-
/// </summary>
49-
/// <value>The date and time when the API version will be deprecated, if any.</value>
50-
public DateTimeOffset? Date { get; }
51-
52-
/// <summary>
53-
/// Gets a value indicating whether the deprecation policy has any associated links.
54-
/// </summary>
55-
/// <value>True if the deprecation policy has associated links; otherwise, false.</value>
56-
public bool HasLinks => links.Count > 0;
57-
58-
/// <summary>
59-
/// Gets a read-only list of links that provide information about the deprecation policy.
60-
/// </summary>
61-
/// <value>A read-only list of HTTP links.</value>
62-
/// <remarks>If a link is provided, generally only one link is necessary; however, additional
63-
/// links might be provided for different languages or different formats such as a HTML page
64-
/// or a JSON file.</remarks>
65-
public IList<LinkHeaderValue> Links => links;
57+
public DeprecationPolicy( LinkHeaderValue link ) => Links.Add( link );
6658
}

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

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,33 @@ namespace Asp.Versioning;
77
/// </summary>
88
public class SunsetPolicy
99
{
10-
private readonly LinkList links;
10+
private LinkList? links;
11+
12+
/// <summary>
13+
/// Gets a read-only list of links that provide information about the sunset policy.
14+
/// </summary>
15+
/// <value>A read-only list of HTTP links.</value>
16+
/// <remarks>If a link is provided, generally only one link is necessary; however, additional
17+
/// links might be provided for different languages or different formats such as a HTML page
18+
/// or a JSON file.</remarks>
19+
public IList<LinkHeaderValue> Links => links ??= new( "sunset" );
20+
21+
/// <summary>
22+
/// Gets a value indicating whether the sunset policy has any associated links.
23+
/// </summary>
24+
/// <value>True if the sunset policy has associated links; otherwise, false.</value>
25+
public bool HasLinks => links is not null && links.Count > 0;
26+
27+
/// <summary>
28+
/// Gets the date and time when the API version will be sunset.
29+
/// </summary>
30+
/// <value>The date and time when the API version will be sunset, if any.</value>
31+
public DateTimeOffset? Date { get; }
1132

1233
/// <summary>
1334
/// Initializes a new instance of the <see cref="SunsetPolicy"/> class.
1435
/// </summary>
15-
public SunsetPolicy()
16-
{
17-
links = new LinkList( "sunset" );
18-
}
36+
public SunsetPolicy() { }
1937

2038
/// <summary>
2139
/// Initializes a new instance of the <see cref="SunsetPolicy"/> class.
@@ -29,38 +47,13 @@ public SunsetPolicy()
2947

3048
if ( link is not null )
3149
{
32-
links.Add( link );
50+
Links.Add( link );
3351
}
3452
}
3553

3654
/// <summary>
3755
/// Initializes a new instance of the <see cref="SunsetPolicy"/> class.
3856
/// </summary>
3957
/// <param name="link">The link which provides information about the sunset policy.</param>
40-
public SunsetPolicy( LinkHeaderValue link )
41-
: this()
42-
{
43-
links.Add( link );
44-
}
45-
46-
/// <summary>
47-
/// Gets the date and time when the API version will be sunset.
48-
/// </summary>
49-
/// <value>The date and time when the API version will be sunset, if any.</value>
50-
public DateTimeOffset? Date { get; }
51-
52-
/// <summary>
53-
/// Gets a value indicating whether the sunset policy has any associated links.
54-
/// </summary>
55-
/// <value>True if the sunset policy has associated links; otherwise, false.</value>
56-
public bool HasLinks => links.Count > 0;
57-
58-
/// <summary>
59-
/// Gets a read-only list of links that provide information about the sunset policy.
60-
/// </summary>
61-
/// <value>A read-only list of HTTP links.</value>
62-
/// <remarks>If a link is provided, generally only one link is necessary; however, additional
63-
/// links might be provided for different languages or different formats such as a HTML page
64-
/// or a JSON file.</remarks>
65-
public IList<LinkHeaderValue> Links => links;
58+
public SunsetPolicy( LinkHeaderValue link ) => Links.Add( link );
6659
}

0 commit comments

Comments
 (0)