Skip to content

Commit b893ad9

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Refactor to use OptionsFactory<T> base implementation
1 parent a668bde commit b893ad9

File tree

1 file changed

+26
-45
lines changed

1 file changed

+26
-45
lines changed

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/ApiExplorerOptionsFactory{T}.cs

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
namespace Asp.Versioning.ApiExplorer;
44

55
using Microsoft.Extensions.Options;
6-
using Option = Microsoft.Extensions.Options.Options;
76

87
/// <summary>
98
/// Represents a factory to create API explorer options.
109
/// </summary>
1110
/// <typeparam name="T">The type of options to create.</typeparam>
1211
[CLSCompliant( false )]
13-
public class ApiExplorerOptionsFactory<T> : IOptionsFactory<T> where T : ApiExplorerOptions, new()
12+
public class ApiExplorerOptionsFactory<T> : OptionsFactory<T> where T : ApiExplorerOptions
1413
{
1514
private readonly IOptions<ApiVersioningOptions> optionsHolder;
1615

@@ -27,60 +26,42 @@ public ApiExplorerOptionsFactory(
2726
IOptions<ApiVersioningOptions> options,
2827
IEnumerable<IConfigureOptions<T>> setups,
2928
IEnumerable<IPostConfigureOptions<T>> postConfigures )
30-
{
31-
optionsHolder = options;
32-
Setups = setups;
33-
PostConfigures = postConfigures;
34-
}
29+
: base( setups, postConfigures ) => optionsHolder = options;
3530

3631
/// <summary>
37-
/// Gets the API versioning options associated with the factory.
38-
/// </summary>
39-
/// <value>The <see cref="ApiVersioningOptions">API versioning options</see> used to create API explorer options.</value>
40-
protected ApiVersioningOptions Options => optionsHolder.Value;
41-
42-
/// <summary>
43-
/// Gets the associated configuration actions to run.
32+
/// Initializes a new instance of the <see cref="ApiExplorerOptionsFactory{T}"/> class.
4433
/// </summary>
45-
/// <value>The <see cref="IEnumerable{T}">sequence</see> of
46-
/// <see cref="IConfigureOptions{TOptions}">configuration actions</see> to run.</value>
47-
protected IEnumerable<IConfigureOptions<T>> Setups { get; }
34+
/// <param name="options">The <see cref="ApiVersioningOptions">API versioning options</see>
35+
/// used to create API explorer options.</param>
36+
/// <param name="setups">The <see cref="IEnumerable{T}">sequence</see> of
37+
/// <see cref="IConfigureOptions{TOptions}">configuration actions</see> to run.</param>
38+
/// <param name="postConfigures">The <see cref="IEnumerable{T}">sequence</see> of
39+
/// <see cref="IPostConfigureOptions{TOptions}">initialization actions</see> to run.</param>
40+
/// <param name="validations">The <see cref="IEnumerable{T}">sequence</see> of
41+
/// <see cref="IValidateOptions{TOptions}">validations</see> to run.</param>
42+
public ApiExplorerOptionsFactory(
43+
IOptions<ApiVersioningOptions> options,
44+
IEnumerable<IConfigureOptions<T>> setups,
45+
IEnumerable<IPostConfigureOptions<T>> postConfigures,
46+
IEnumerable<IValidateOptions<T>> validations )
47+
: base( setups, postConfigures, validations ) => optionsHolder = options;
4848

4949
/// <summary>
50-
/// Gets the associated initialization actions to run.
50+
/// Gets the API versioning options associated with the factory.
5151
/// </summary>
52-
/// <value>The <see cref="IEnumerable{T}">sequence</see> of
53-
/// <see cref="IPostConfigureOptions{TOptions}">initialization actions</see> to run.</value>
54-
protected IEnumerable<IPostConfigureOptions<T>> PostConfigures { get; }
52+
/// <value>The <see cref="ApiVersioningOptions">API versioning options</see> used to create API explorer options.</value>
53+
protected ApiVersioningOptions Options => optionsHolder.Value;
5554

5655
/// <inheritdoc />
57-
public virtual T Create( string name )
56+
protected override T CreateInstance( string name )
5857
{
5958
var apiVersioningOptions = Options;
60-
var options = new T()
61-
{
62-
AssumeDefaultVersionWhenUnspecified = apiVersioningOptions.AssumeDefaultVersionWhenUnspecified,
63-
ApiVersionParameterSource = apiVersioningOptions.ApiVersionReader,
64-
DefaultApiVersion = apiVersioningOptions.DefaultApiVersion,
65-
RouteConstraintName = apiVersioningOptions.RouteConstraintName,
66-
};
67-
68-
foreach ( var setup in Setups )
69-
{
70-
if ( setup is IConfigureNamedOptions<T> namedSetup )
71-
{
72-
namedSetup.Configure( name, options );
73-
}
74-
else if ( name == Option.DefaultName )
75-
{
76-
setup.Configure( options );
77-
}
78-
}
59+
var options = base.CreateInstance( name );
7960

80-
foreach ( var post in PostConfigures )
81-
{
82-
post.PostConfigure( name, options );
83-
}
61+
options.AssumeDefaultVersionWhenUnspecified = apiVersioningOptions.AssumeDefaultVersionWhenUnspecified;
62+
options.ApiVersionParameterSource = apiVersioningOptions.ApiVersionReader;
63+
options.DefaultApiVersion = apiVersioningOptions.DefaultApiVersion;
64+
options.RouteConstraintName = apiVersioningOptions.RouteConstraintName;
8465

8566
return options;
8667
}

0 commit comments

Comments
 (0)