Resvolution - ValidatorsResolver
This commit is contained in:
@@ -407,17 +407,14 @@ namespace Umbraco.Core
|
||||
ParameterEditorCollectionBuilder.Register(Container)
|
||||
.AddProducer(() => PluginManager.ResolveParameterEditors());
|
||||
|
||||
//setup the validators resolver with our predefined validators
|
||||
ValidatorsResolver.Current = new ValidatorsResolver(
|
||||
ServiceProvider, ProfilingLogger.Logger, new[]
|
||||
{
|
||||
new Lazy<Type>(() => typeof (RequiredManifestValueValidator)),
|
||||
new Lazy<Type>(() => typeof (RegexValidator)),
|
||||
new Lazy<Type>(() => typeof (DelimitedManifestValueValidator)),
|
||||
new Lazy<Type>(() => typeof (EmailValidator)),
|
||||
new Lazy<Type>(() => typeof (IntegerValidator)),
|
||||
new Lazy<Type>(() => typeof (DecimalValidator)),
|
||||
});
|
||||
// setup validators with our predefined validators
|
||||
ValidatorCollectionBuilder.Register(Container)
|
||||
.Add<RequiredManifestValueValidator>()
|
||||
.Add<RegexValidator>()
|
||||
.Add<DelimitedManifestValueValidator>()
|
||||
.Add<EmailValidator>()
|
||||
.Add<IntegerValidator>()
|
||||
.Add<DecimalValidator>();
|
||||
|
||||
//by default we'll use the db server registrar unless the developer has the legacy
|
||||
// dist calls enabled, in which case we'll use the config server registrar
|
||||
|
||||
@@ -53,6 +53,9 @@ namespace Umbraco.Core.DependencyInjection
|
||||
public static ParameterEditorCollection ParameterEditors
|
||||
=> Container.GetInstance<ParameterEditorCollection>();
|
||||
|
||||
internal static ValidatorCollection Validators
|
||||
=> Container.GetInstance<ValidatorCollection>();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Newtonsoft.Json;
|
||||
using Umbraco.Core.DependencyInjection;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Serialization;
|
||||
|
||||
@@ -39,7 +40,7 @@ namespace Umbraco.Core.PropertyEditors
|
||||
{
|
||||
if (_validatorInstance == null)
|
||||
{
|
||||
var val = ValidatorsResolver.Current.GetValidator(Type);
|
||||
var val = Current.Validators[Type];
|
||||
if (val == null)
|
||||
{
|
||||
throw new InvalidOperationException("No " + typeof(ManifestValueValidator) + " could be found for the type name of " + Type);
|
||||
|
||||
15
src/Umbraco.Core/PropertyEditors/ValidatorCollection.cs
Normal file
15
src/Umbraco.Core/PropertyEditors/ValidatorCollection.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.DependencyInjection;
|
||||
|
||||
namespace Umbraco.Core.PropertyEditors
|
||||
{
|
||||
internal class ValidatorCollection : BuilderCollectionBase<ManifestValueValidator>
|
||||
{
|
||||
public ValidatorCollection(IEnumerable<ManifestValueValidator> items)
|
||||
: base(items)
|
||||
{ }
|
||||
|
||||
public ManifestValueValidator this[string name] => this.FirstOrDefault(x => x.TypeName.InvariantEquals(name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using LightInject;
|
||||
using Umbraco.Core.DependencyInjection;
|
||||
|
||||
namespace Umbraco.Core.PropertyEditors
|
||||
{
|
||||
internal class ValidatorCollectionBuilder : LazyCollectionBuilderBase<ValidatorCollectionBuilder, ValidatorCollection, ManifestValueValidator>
|
||||
{
|
||||
public ValidatorCollectionBuilder(IServiceContainer container)
|
||||
: base(container)
|
||||
{ }
|
||||
|
||||
protected override ValidatorCollectionBuilder This => this;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.ObjectResolution;
|
||||
|
||||
namespace Umbraco.Core.PropertyEditors
|
||||
{
|
||||
/// <summary>
|
||||
/// A resolver to resolve all registered validators
|
||||
/// </summary>
|
||||
internal class ValidatorsResolver : LazyManyObjectsResolverBase<ValidatorsResolver, ManifestValueValidator>
|
||||
{
|
||||
public ValidatorsResolver(IServiceProvider serviceProvider, ILogger logger, IEnumerable<Lazy<Type>> lazyTypeList)
|
||||
: base(serviceProvider, logger, lazyTypeList, ObjectLifetimeScope.Application)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the validators
|
||||
/// </summary>
|
||||
public IEnumerable<ManifestValueValidator> Validators
|
||||
{
|
||||
get { return Values; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a validator by name
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public ManifestValueValidator GetValidator(string name)
|
||||
{
|
||||
return Values.FirstOrDefault(x => x.TypeName.InvariantEquals(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -453,6 +453,8 @@
|
||||
<Compile Include="PropertyEditors\PropertyEditorCollection.cs" />
|
||||
<Compile Include="PropertyEditors\PropertyEditorCollectionBuilder.cs" />
|
||||
<Compile Include="PropertyEditors\PropertyEditorValueTypes.cs" />
|
||||
<Compile Include="PropertyEditors\ValidatorCollection.cs" />
|
||||
<Compile Include="PropertyEditors\ValidatorCollectionBuilder.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\GridValueConverter.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\DecimalValueConverter.cs" />
|
||||
<Compile Include="PropertyEditors\ValueConverters\LabelValueConverter.cs" />
|
||||
@@ -1049,7 +1051,6 @@
|
||||
<Compile Include="PropertyEditors\RegexValidator.cs" />
|
||||
<Compile Include="PropertyEditors\RequiredManifestValueValidator.cs" />
|
||||
<Compile Include="PropertyEditors\IPropertyValidator.cs" />
|
||||
<Compile Include="PropertyEditors\ValidatorsResolver.cs" />
|
||||
<Compile Include="PropertyEditors\PropertyValueEditor.cs" />
|
||||
<Compile Include="PropertyEditors\ValueValidatorAttribute.cs" />
|
||||
<Compile Include="Dictionary\ICultureDictionary.cs" />
|
||||
|
||||
@@ -158,6 +158,9 @@ namespace Umbraco.Web
|
||||
public static ParameterEditorCollection ParameterEditors
|
||||
=> Container.GetInstance<ParameterEditorCollection>();
|
||||
|
||||
internal static ValidatorCollection Validators
|
||||
=> Container.GetInstance<ValidatorCollection>();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user