From dbd76a903a3de546ed219fb0aa99558d9bdab6e8 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 19 Nov 2019 15:25:04 +1100 Subject: [PATCH] Cleans up the IPropertyType interface, cleans up how it is constructed --- src/Umbraco.Abstractions/Models/IProperty.cs | 7 +-- .../Models/PropertyTypeCollection.cs | 5 +- src/Umbraco.Core/Models/Property.cs | 50 ++++++++++++++++++- .../Persistence/Factories/PropertyFactory.cs | 30 ++++------- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/Umbraco.Abstractions/Models/IProperty.cs b/src/Umbraco.Abstractions/Models/IProperty.cs index 3a0d984e55..44e84d9b68 100644 --- a/src/Umbraco.Abstractions/Models/IProperty.cs +++ b/src/Umbraco.Abstractions/Models/IProperty.cs @@ -33,14 +33,9 @@ namespace Umbraco.Core.Models /// void SetValue(object value, string culture = null, string segment = null); - /// - /// Resets the entity identity. - /// - void ResetIdentity(); - int PropertyTypeId { get; } void PublishValues(string culture = "*", string segment = "*"); void UnpublishValues(string culture = "*", string segment = "*"); - void FactorySetValue(string culture, string segment, bool published, object value); + } } diff --git a/src/Umbraco.Abstractions/Models/PropertyTypeCollection.cs b/src/Umbraco.Abstractions/Models/PropertyTypeCollection.cs index 5d60241a9c..d26a092c64 100644 --- a/src/Umbraco.Abstractions/Models/PropertyTypeCollection.cs +++ b/src/Umbraco.Abstractions/Models/PropertyTypeCollection.cs @@ -16,7 +16,7 @@ namespace Umbraco.Core.Models [Serializable] [DataContract] // TODO: Change this to ObservableDictionary so we can reduce the INotifyCollectionChanged implementation details - public class PropertyTypeCollection : KeyedCollection, INotifyCollectionChanged, IDeepCloneable + public class PropertyTypeCollection : KeyedCollection, INotifyCollectionChanged, IDeepCloneable, ICollection { [IgnoreDataMember] private readonly ReaderWriterLockSlim _addLocker = new ReaderWriterLockSlim(); @@ -83,8 +83,7 @@ namespace Umbraco.Core.Models OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } - // TODO: Instead of 'new' this should explicitly implement one of the collection interfaces members - public new void Add(IPropertyType item) + void ICollection.Add(IPropertyType item) { item.SupportsPublishing = SupportsPublishing; diff --git a/src/Umbraco.Core/Models/Property.cs b/src/Umbraco.Core/Models/Property.cs index e75a7420a8..caf3bcc167 100644 --- a/src/Umbraco.Core/Models/Property.cs +++ b/src/Umbraco.Core/Models/Property.cs @@ -47,6 +47,54 @@ namespace Umbraco.Core.Models PropertyType = propertyType; } + /// + /// Creates a new instance for existing + /// + /// + /// + /// + /// Generally will contain a published and an unpublished property values + /// + /// + public static Property CreateWithValues(int id, IPropertyType propertyType, params InitialPropertyValue[] values) + { + var property = new Property(propertyType); + try + { + property.DisableChangeTracking(); + property.Id = id; + foreach(var value in values) + { + property.FactorySetValue(value.Culture, value.Segment, value.Published, value.Value); + } + property.ResetDirtyProperties(false); + return property; + } + finally + { + property.EnableChangeTracking(); + } + } + + /// + /// Used for constructing a new instance + /// + public class InitialPropertyValue + { + public InitialPropertyValue(string culture, string segment, bool published, object value) + { + Culture = culture ?? throw new ArgumentNullException(nameof(culture)); + Segment = segment ?? throw new ArgumentNullException(nameof(segment)); + Published = published; + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public string Culture { get; } + public string Segment { get; } + public bool Published { get; } + public object Value { get; } + } + /// /// Represents a property value. /// @@ -284,7 +332,7 @@ namespace Umbraco.Core.Models } // bypasses all changes detection and is the *only* way to set the published value - public void FactorySetValue(string culture, string segment, bool published, object value) + private void FactorySetValue(string culture, string segment, bool published, object value) { var (pvalue, _) = GetPValue(culture, segment, true); diff --git a/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs b/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs index e92c379f07..f844d14022 100644 --- a/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs @@ -16,31 +16,21 @@ namespace Umbraco.Core.Persistence.Factories foreach (var propertyType in propertyTypes) { - var property = new Property(propertyType); + var values = new List(); + int propertyId = default; - try + // see notes in BuildDtos - we always have edit+published dtos + if (xdtos.TryGetValue(propertyType.Id, out var propDtos)) { - property.DisableChangeTracking(); - - // see notes in BuildDtos - we always have edit+published dtos - - if (xdtos.TryGetValue(propertyType.Id, out var propDtos)) + foreach (var propDto in propDtos) { - foreach (var propDto in propDtos) - { - property.Id = propDto.Id; - property.FactorySetValue(languageRepository.GetIsoCodeById(propDto.LanguageId), propDto.Segment, propDto.VersionId == publishedVersionId, propDto.Value); - } - + propertyId = propDto.Id; + values.Add(new Property.InitialPropertyValue(languageRepository.GetIsoCodeById(propDto.LanguageId), propDto.Segment, propDto.VersionId == publishedVersionId, propDto.Value)); } + } - property.ResetDirtyProperties(false); - properties.Add(property); - } - finally - { - property.EnableChangeTracking(); - } + var property = Property.CreateWithValues(propertyId, propertyType, values.ToArray()); + properties.Add(property); } return properties;