Skip to content

Remove internal new() constraints from options infrastructure #2169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public partial interface IOptionsChangeTokenSource<out TOptions>
string Name { get; }
Microsoft.Extensions.Primitives.IChangeToken GetChangeToken();
}
public partial interface IOptionsFactory<TOptions> where TOptions : class, new()
public partial interface IOptionsFactory<TOptions> where TOptions : class
{
TOptions Create(string name);
}
Expand All @@ -120,11 +120,11 @@ public partial interface IOptionsMonitor<out TOptions>
TOptions Get(string name);
System.IDisposable OnChange(System.Action<TOptions, string> listener);
}
public partial interface IOptionsSnapshot<out TOptions> : Microsoft.Extensions.Options.IOptions<TOptions> where TOptions : class, new()
public partial interface IOptionsSnapshot<out TOptions> : Microsoft.Extensions.Options.IOptions<TOptions> where TOptions : class
{
TOptions Get(string name);
}
public partial interface IOptions<out TOptions> where TOptions : class, new()
public partial interface IOptions<out TOptions> where TOptions : class
{
TOptions Value { get; }
}
Expand All @@ -139,7 +139,7 @@ public partial interface IValidateOptions<TOptions> where TOptions : class
public static partial class Options
{
public static readonly string DefaultName;
public static Microsoft.Extensions.Options.IOptions<TOptions> Create<TOptions>(TOptions options) where TOptions : class, new() { throw null; }
public static Microsoft.Extensions.Options.IOptions<TOptions> Create<TOptions>(TOptions options) where TOptions : class { throw null; }
}
public partial class OptionsBuilder<TOptions> where TOptions : class
{
Expand Down Expand Up @@ -179,13 +179,14 @@ public void Clear() { }
public virtual bool TryAdd(string name, TOptions options) { throw null; }
public virtual bool TryRemove(string name) { throw null; }
}
public partial class OptionsFactory<TOptions> : Microsoft.Extensions.Options.IOptionsFactory<TOptions> where TOptions : class, new()
public partial class OptionsFactory<TOptions> : Microsoft.Extensions.Options.IOptionsFactory<TOptions> where TOptions : class
{
public OptionsFactory(System.Collections.Generic.IEnumerable<Microsoft.Extensions.Options.IConfigureOptions<TOptions>> setups, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Options.IPostConfigureOptions<TOptions>> postConfigures) { }
public OptionsFactory(System.Collections.Generic.IEnumerable<Microsoft.Extensions.Options.IConfigureOptions<TOptions>> setups, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Options.IPostConfigureOptions<TOptions>> postConfigures, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Options.IValidateOptions<TOptions>> validations) { }
public TOptions Create(string name) { throw null; }
protected virtual TOptions CreateInstance(string name) { throw null; }
}
public partial class OptionsManager<TOptions> : Microsoft.Extensions.Options.IOptions<TOptions>, Microsoft.Extensions.Options.IOptionsSnapshot<TOptions> where TOptions : class, new()
public partial class OptionsManager<TOptions> : Microsoft.Extensions.Options.IOptions<TOptions>, Microsoft.Extensions.Options.IOptionsSnapshot<TOptions> where TOptions : class
{
public OptionsManager(Microsoft.Extensions.Options.IOptionsFactory<TOptions> factory) { }
public TOptions Value { get { throw null; } }
Expand All @@ -195,7 +196,7 @@ public static partial class OptionsMonitorExtensions
{
public static System.IDisposable OnChange<TOptions>(this Microsoft.Extensions.Options.IOptionsMonitor<TOptions> monitor, System.Action<TOptions> listener) { throw null; }
}
public partial class OptionsMonitor<TOptions> : Microsoft.Extensions.Options.IOptionsMonitor<TOptions>, System.IDisposable where TOptions : class, new()
public partial class OptionsMonitor<TOptions> : Microsoft.Extensions.Options.IOptionsMonitor<TOptions>, System.IDisposable where TOptions : class
{
public OptionsMonitor(Microsoft.Extensions.Options.IOptionsFactory<TOptions> factory, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Options.IOptionsChangeTokenSource<TOptions>> sources, Microsoft.Extensions.Options.IOptionsMonitorCache<TOptions> cache) { }
public TOptions CurrentValue { get { throw null; } }
Expand All @@ -211,7 +212,7 @@ public OptionsValidationException(string optionsName, System.Type optionsType, S
public string OptionsName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
public System.Type OptionsType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
}
public partial class OptionsWrapper<TOptions> : Microsoft.Extensions.Options.IOptions<TOptions> where TOptions : class, new()
public partial class OptionsWrapper<TOptions> : Microsoft.Extensions.Options.IOptions<TOptions> where TOptions : class
{
public OptionsWrapper(TOptions options) { }
public TOptions Value { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
Expand Down
2 changes: 1 addition & 1 deletion src/Options/Options/src/IOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.Options
/// Used to retrieve configured <typeparamref name="TOptions"/> instances.
/// </summary>
/// <typeparam name="TOptions">The type of options being requested.</typeparam>
public interface IOptions<out TOptions> where TOptions : class, new()
public interface IOptions<out TOptions> where TOptions : class
{
/// <summary>
/// The default configured <typeparamref name="TOptions"/> instance
Expand Down
2 changes: 1 addition & 1 deletion src/Options/Options/src/IOptionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.Options
/// Used to create <typeparamref name="TOptions"/> instances.
/// </summary>
/// <typeparam name="TOptions">The type of options being requested.</typeparam>
public interface IOptionsFactory<TOptions> where TOptions : class, new()
public interface IOptionsFactory<TOptions> where TOptions : class
{
/// <summary>
/// Returns a configured <typeparamref name="TOptions"/> instance with the given name.
Expand Down
2 changes: 1 addition & 1 deletion src/Options/Options/src/IOptionsSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.Options
/// Used to access the value of <typeparamref name="TOptions"/> for the lifetime of a request.
/// </summary>
/// <typeparam name="TOptions">Options type.</typeparam>
public interface IOptionsSnapshot<out TOptions> : IOptions<TOptions> where TOptions : class, new()
public interface IOptionsSnapshot<out TOptions> : IOptions<TOptions> where TOptions : class
{
/// <summary>
/// Returns a configured <typeparamref name="TOptions"/> instance with the given name.
Expand Down
2 changes: 1 addition & 1 deletion src/Options/Options/src/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class Options
/// <typeparam name="TOptions">Options type.</typeparam>
/// <param name="options">Options object.</param>
/// <returns>Wrapped options object.</returns>
public static IOptions<TOptions> Create<TOptions>(TOptions options) where TOptions : class, new()
public static IOptions<TOptions> Create<TOptions>(TOptions options) where TOptions : class
{
return new OptionsWrapper<TOptions>(options);
}
Expand Down
13 changes: 11 additions & 2 deletions src/Options/Options/src/OptionsFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
using System.Collections.Generic;

namespace Microsoft.Extensions.Options
Expand All @@ -9,7 +10,7 @@ namespace Microsoft.Extensions.Options
/// Implementation of <see cref="IOptionsFactory{TOptions}"/>.
/// </summary>
/// <typeparam name="TOptions">The type of options being requested.</typeparam>
public class OptionsFactory<TOptions> : IOptionsFactory<TOptions> where TOptions : class, new()
public class OptionsFactory<TOptions> : IOptionsFactory<TOptions> where TOptions : class
{
private readonly IEnumerable<IConfigureOptions<TOptions>> _setups;
private readonly IEnumerable<IPostConfigureOptions<TOptions>> _postConfigures;
Expand Down Expand Up @@ -41,7 +42,7 @@ public OptionsFactory(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerab
/// </summary>
public TOptions Create(string name)
{
var options = new TOptions();
var options = CreateInstance(name);
foreach (var setup in _setups)
{
if (setup is IConfigureNamedOptions<TOptions> namedSetup)
Expand Down Expand Up @@ -77,5 +78,13 @@ public TOptions Create(string name)

return options;
}

/// <summary>
/// Creates a new instance of options type
/// </summary>
protected virtual TOptions CreateInstance(string name)
{
return Activator.CreateInstance<TOptions>();
}
}
}
2 changes: 1 addition & 1 deletion src/Options/Options/src/OptionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.Options
/// Implementation of <see cref="IOptions{TOptions}"/> and <see cref="IOptionsSnapshot{TOptions}"/>.
/// </summary>
/// <typeparam name="TOptions">Options type.</typeparam>
public class OptionsManager<TOptions> : IOptions<TOptions>, IOptionsSnapshot<TOptions> where TOptions : class, new()
public class OptionsManager<TOptions> : IOptions<TOptions>, IOptionsSnapshot<TOptions> where TOptions : class
{
private readonly IOptionsFactory<TOptions> _factory;
private readonly OptionsCache<TOptions> _cache = new OptionsCache<TOptions>(); // Note: this is a private cache
Expand Down
2 changes: 1 addition & 1 deletion src/Options/Options/src/OptionsMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Extensions.Options
/// Implementation of <see cref="IOptionsMonitor{TOptions}"/>.
/// </summary>
/// <typeparam name="TOptions">Options type.</typeparam>
public class OptionsMonitor<TOptions> : IOptionsMonitor<TOptions>, IDisposable where TOptions : class, new()
public class OptionsMonitor<TOptions> : IOptionsMonitor<TOptions>, IDisposable where TOptions : class
{
private readonly IOptionsMonitorCache<TOptions> _cache;
private readonly IOptionsFactory<TOptions> _factory;
Expand Down
2 changes: 1 addition & 1 deletion src/Options/Options/src/OptionsWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.Extensions.Options
/// <see cref="IOptions{TOptions}"/> wrapper that returns the options instance.
/// </summary>
/// <typeparam name="TOptions">Options type.</typeparam>
public class OptionsWrapper<TOptions> : IOptions<TOptions> where TOptions : class, new()
public class OptionsWrapper<TOptions> : IOptions<TOptions> where TOptions : class
{
/// <summary>
/// Intializes the wrapper with the options instance to return.
Expand Down
54 changes: 54 additions & 0 deletions src/Options/test/OptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
Expand Down Expand Up @@ -343,5 +344,58 @@ public void Options_CanOverrideForSpecificTOptions()
var sp = services.BuildServiceProvider();
Assert.Equal("Override", sp.GetRequiredService<IOptions<FakeOptions>>().Value.Message);
}

[Fact]
public void Options_CanCreateInstancesWithoutDefaultCtor()
{
var services = new ServiceCollection();
services.Configure<OptionsWithoutDefaultCtor>("Named", options =>
{
options.Message = "Initial value";
});

services.AddSingleton<IOptionsFactory<OptionsWithoutDefaultCtor>, CustomOptionsFactory>();

var sp = services.BuildServiceProvider();
var optionsWithoutDefaultCtor = sp.GetRequiredService<IOptionsMonitor<OptionsWithoutDefaultCtor>>().Get("Named");
Assert.Equal("Initial value", optionsWithoutDefaultCtor.Message);
Assert.Equal("Named", optionsWithoutDefaultCtor.Name);
}

[Fact]
public void Options_WithoutDefaultCtor_ThrowDuringResolution()
{
var services = new ServiceCollection();
services.Configure<OptionsWithoutDefaultCtor>("Named", options =>
{
options.Message = "Initial value";
});

var sp = services.BuildServiceProvider();
Assert.Throws<MissingMethodException>(() => sp.GetRequiredService<IOptionsMonitor<OptionsWithoutDefaultCtor>>().Get("Named"));
}

private class OptionsWithoutDefaultCtor
{
public string Name { get; }
public string Message { get; set; }

public OptionsWithoutDefaultCtor(string name)
{
Name = name;
}
}

private class CustomOptionsFactory: OptionsFactory<OptionsWithoutDefaultCtor>
{
public CustomOptionsFactory(IEnumerable<IConfigureOptions<OptionsWithoutDefaultCtor>> setups, IEnumerable<IPostConfigureOptions<OptionsWithoutDefaultCtor>> postConfigures, IEnumerable<IValidateOptions<OptionsWithoutDefaultCtor>> validations) : base(setups, postConfigures, validations)
{
}

protected override OptionsWithoutDefaultCtor CreateInstance(string name)
{
return new OptionsWithoutDefaultCtor(name);
}
}
}
}