-
-
Save configureappio/6784ae9d89341970874767f94034d976 to your computer and use it in GitHub Desktop.
OptionsFactory.cs from https://github.com/aspnet/Options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) .NET Foundation. All rights reserved. | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | |
using System.Collections.Generic; | |
namespace Microsoft.Extensions.Options | |
{ | |
/// <summary> | |
/// Implementation of IOptionsFactory. | |
/// </summary> | |
/// <typeparam name="TOptions">The type of options being requested.</typeparam> | |
public class OptionsFactory<TOptions> : IOptionsFactory<TOptions> where TOptions : class, new() | |
{ | |
private readonly IEnumerable<IConfigureOptions<TOptions>> _setups; | |
private readonly IEnumerable<IPostConfigureOptions<TOptions>> _postConfigures; | |
/// <summary> | |
/// Initializes a new instance with the specified options configurations. | |
/// </summary> | |
/// <param name="setups">The configuration actions to run.</param> | |
/// <param name="postConfigures">The initialization actions to run.</param> | |
public OptionsFactory(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerable<IPostConfigureOptions<TOptions>> postConfigures) | |
{ | |
_setups = setups; | |
_postConfigures = postConfigures; | |
} | |
public TOptions Create(string name) | |
{ | |
var options = new TOptions(); | |
foreach (var setup in _setups) | |
{ | |
if (setup is IConfigureNamedOptions<TOptions> namedSetup) | |
{ | |
namedSetup.Configure(name, options); | |
} | |
else if (name == Options.DefaultName) | |
{ | |
setup.Configure(options); | |
} | |
} | |
foreach (var post in _postConfigures) | |
{ | |
post.PostConfigure(name, options); | |
} | |
return options; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment