From a45ffb71a4d22972fed050938ca944fdeda08c6d Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 18 Aug 2016 10:19:33 +0200 Subject: [PATCH] Resvolution - ValidatorsResolver --- src/Umbraco.Core/CoreBootManager.cs | 19 ++++------ .../DependencyInjection/Current.cs | 3 ++ .../ManifestPropertyValidator.cs | 3 +- .../PropertyEditors/ValidatorCollection.cs | 15 ++++++++ .../ValidatorCollectionBuilder.cs | 19 ++++++++++ .../PropertyEditors/ValidatorsResolver.cs | 37 ------------------- src/Umbraco.Core/Umbraco.Core.csproj | 3 +- src/Umbraco.Web/Current.cs | 3 ++ 8 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 src/Umbraco.Core/PropertyEditors/ValidatorCollection.cs create mode 100644 src/Umbraco.Core/PropertyEditors/ValidatorCollectionBuilder.cs delete mode 100644 src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index eb87439689..a4ff475b70 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -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(() => typeof (RequiredManifestValueValidator)), - new Lazy(() => typeof (RegexValidator)), - new Lazy(() => typeof (DelimitedManifestValueValidator)), - new Lazy(() => typeof (EmailValidator)), - new Lazy(() => typeof (IntegerValidator)), - new Lazy(() => typeof (DecimalValidator)), - }); + // setup validators with our predefined validators + ValidatorCollectionBuilder.Register(Container) + .Add() + .Add() + .Add() + .Add() + .Add() + .Add(); //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 diff --git a/src/Umbraco.Core/DependencyInjection/Current.cs b/src/Umbraco.Core/DependencyInjection/Current.cs index 30d7631c8a..5e4364c3bf 100644 --- a/src/Umbraco.Core/DependencyInjection/Current.cs +++ b/src/Umbraco.Core/DependencyInjection/Current.cs @@ -53,6 +53,9 @@ namespace Umbraco.Core.DependencyInjection public static ParameterEditorCollection ParameterEditors => Container.GetInstance(); + internal static ValidatorCollection Validators + => Container.GetInstance(); + #endregion } } diff --git a/src/Umbraco.Core/PropertyEditors/ManifestPropertyValidator.cs b/src/Umbraco.Core/PropertyEditors/ManifestPropertyValidator.cs index f0a1532d99..6249b470fc 100644 --- a/src/Umbraco.Core/PropertyEditors/ManifestPropertyValidator.cs +++ b/src/Umbraco.Core/PropertyEditors/ManifestPropertyValidator.cs @@ -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); diff --git a/src/Umbraco.Core/PropertyEditors/ValidatorCollection.cs b/src/Umbraco.Core/PropertyEditors/ValidatorCollection.cs new file mode 100644 index 0000000000..d9be27ee42 --- /dev/null +++ b/src/Umbraco.Core/PropertyEditors/ValidatorCollection.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Linq; +using Umbraco.Core.DependencyInjection; + +namespace Umbraco.Core.PropertyEditors +{ + internal class ValidatorCollection : BuilderCollectionBase + { + public ValidatorCollection(IEnumerable items) + : base(items) + { } + + public ManifestValueValidator this[string name] => this.FirstOrDefault(x => x.TypeName.InvariantEquals(name)); + } +} diff --git a/src/Umbraco.Core/PropertyEditors/ValidatorCollectionBuilder.cs b/src/Umbraco.Core/PropertyEditors/ValidatorCollectionBuilder.cs new file mode 100644 index 0000000000..e41bed4f06 --- /dev/null +++ b/src/Umbraco.Core/PropertyEditors/ValidatorCollectionBuilder.cs @@ -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 + { + public ValidatorCollectionBuilder(IServiceContainer container) + : base(container) + { } + + protected override ValidatorCollectionBuilder This => this; + } +} diff --git a/src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs b/src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs deleted file mode 100644 index 547271a941..0000000000 --- a/src/Umbraco.Core/PropertyEditors/ValidatorsResolver.cs +++ /dev/null @@ -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 -{ - /// - /// A resolver to resolve all registered validators - /// - internal class ValidatorsResolver : LazyManyObjectsResolverBase - { - public ValidatorsResolver(IServiceProvider serviceProvider, ILogger logger, IEnumerable> lazyTypeList) - : base(serviceProvider, logger, lazyTypeList, ObjectLifetimeScope.Application) - { - } - - /// - /// Returns the validators - /// - public IEnumerable Validators - { - get { return Values; } - } - - /// - /// Gets a validator by name - /// - /// - /// - public ManifestValueValidator GetValidator(string name) - { - return Values.FirstOrDefault(x => x.TypeName.InvariantEquals(name)); - } - } -} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 6181d5bf90..8318c269e7 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -453,6 +453,8 @@ + + @@ -1049,7 +1051,6 @@ - diff --git a/src/Umbraco.Web/Current.cs b/src/Umbraco.Web/Current.cs index 18c87c5a3b..945f0bd887 100644 --- a/src/Umbraco.Web/Current.cs +++ b/src/Umbraco.Web/Current.cs @@ -158,6 +158,9 @@ namespace Umbraco.Web public static ParameterEditorCollection ParameterEditors => Container.GetInstance(); + internal static ValidatorCollection Validators + => Container.GetInstance(); + #endregion } }