Refactors Configs and how it acquires a factory, re-internalizes Umbraco.Configuration classes

This commit is contained in:
Shannon
2019-11-19 11:16:07 +11:00
parent 21e9d09ff0
commit 8f48df66d5
6 changed files with 12 additions and 20 deletions

View File

@@ -124,14 +124,14 @@ namespace Umbraco.Core.Composing
builder.RegisterWith(_register);
_builders.Clear(); // no point keep them around
Configs.RegisterWith(_register);
IFactory factory = null;
Configs.RegisterWith(_register, () => factory);
// ReSharper disable once AccessToModifiedClosure -- on purpose
_register.Register(_ => factory, Lifetime.Singleton);
factory = _register.CreateFactory();
Configs.Factory = factory;
return factory;
}

View File

@@ -15,16 +15,6 @@ namespace Umbraco.Core.Configuration
{
private readonly Func<string, object> _configGetter;
public IFactory Factory
{
get => _factory;
set
{
if(!(_factory is null)) throw new InvalidOperationException("Factory cannot be set multiple times");
_factory = value;
}
}
public Configs(Func<string, object> configGetter)
{
_configGetter = configGetter;
@@ -32,7 +22,7 @@ namespace Umbraco.Core.Configuration
private readonly Dictionary<Type, Lazy<object>> _configs = new Dictionary<Type, Lazy<object>>();
private Dictionary<Type, Action<IRegister>> _registerings = new Dictionary<Type, Action<IRegister>>();
private IFactory _factory;
private Lazy<IFactory> _factory;
/// <summary>
/// Gets a configuration.
@@ -76,7 +66,7 @@ namespace Umbraco.Core.Configuration
_configs[typeOfConfig] = new Lazy<object>(() =>
{
if (!(Factory is null)) return Factory.GetInstance<TConfig>();
if (!(_factory is null)) return _factory.Value.GetInstance<TConfig>();
throw new InvalidOperationException($"Cannot get configuration of type {typeOfConfig} during composition.");
});
_registerings[typeOfConfig] = register => register.Register(configFactory, Lifetime.Singleton);
@@ -109,12 +99,14 @@ namespace Umbraco.Core.Configuration
/// <summary>
/// Registers configurations in a register.
/// </summary>
public void RegisterWith(IRegister register)
public void RegisterWith(IRegister register, Func<IFactory> factory)
{
// do it only once
if (_registerings == null)
throw new InvalidOperationException("Configurations have already been registered.");
_factory = new Lazy<IFactory>(factory);
register.Register(this);
foreach (var registering in _registerings.Values)

View File

@@ -8,7 +8,7 @@ namespace Umbraco.Core.Configuration
/// <summary>
/// Defines a configuration section that contains inner text that is comma delimited
/// </summary>
public class CommaDelimitedConfigurationElement : InnerTextConfigurationElement<CommaDelimitedStringCollection>, IEnumerable<string>
internal class CommaDelimitedConfigurationElement : InnerTextConfigurationElement<CommaDelimitedStringCollection>, IEnumerable<string>
{
public override CommaDelimitedStringCollection Value
{

View File

@@ -9,7 +9,7 @@ namespace Umbraco.Core.Configuration
/// {element}MyValue{/element} instead of as attribute values.
/// </summary>
/// <typeparam name="T"></typeparam>
public class InnerTextConfigurationElement<T> : RawXmlConfigurationElement
internal class InnerTextConfigurationElement<T> : RawXmlConfigurationElement
{
public InnerTextConfigurationElement()
{

View File

@@ -6,7 +6,7 @@ namespace Umbraco.Core.Configuration
/// <summary>
/// Used for specifying default values for comma delimited config
/// </summary>
public class OptionalCommaDelimitedConfigurationElement : CommaDelimitedConfigurationElement
internal class OptionalCommaDelimitedConfigurationElement : CommaDelimitedConfigurationElement
{
private readonly CommaDelimitedConfigurationElement _wrapped;
private readonly string[] _defaultValue;

View File

@@ -8,7 +8,7 @@ namespace Umbraco.Core.Configuration
/// A configuration section that simply exposes the entire raw xml of the section itself which inheritors can use
/// to do with as they please.
/// </summary>
public abstract class RawXmlConfigurationElement : ConfigurationElement
internal abstract class RawXmlConfigurationElement : ConfigurationElement
{
protected RawXmlConfigurationElement()
{