diff --git a/src/Umbraco.Core/ContentTypeBaseServiceProviderExtensions.cs b/src/Umbraco.Core/ContentTypeBaseServiceProviderExtensions.cs deleted file mode 100644 index f962d9aefb..0000000000 --- a/src/Umbraco.Core/ContentTypeBaseServiceProviderExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using Umbraco.Core.Models; -using Umbraco.Core.Services; - -namespace Umbraco.Core -{ - internal static class ContentTypeBaseServiceProviderExtensions - { - //TODO: Maybe this should just be on the IContentTypeBaseServiceProvider interface? - public static IContentTypeComposition GetContentTypeOf(this IContentTypeBaseServiceProvider serviceProvider, IContentBase contentBase) - { - if (contentBase == null) throw new ArgumentNullException(nameof(contentBase)); - return serviceProvider.For(contentBase)?.Get(contentBase.ContentTypeId); - } - } -} diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index e6e893d905..fbb68194b7 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -307,26 +307,10 @@ namespace Umbraco.Core.Models /// public void SetValue(string propertyTypeAlias, object value, string culture = null, string segment = null) { - if (Properties.TryGetValue(propertyTypeAlias, out var property)) - { - property.SetValue(value, culture, segment); - } - else - { - //fixme: Can this ever happen? According to the ctor in ContentBase (EnsurePropertyTypes), all properties will be created based on the content type's property types - // so how can a property not be resolved by the alias on the content.Properties but it can on the content type? - // This maybe can happen if a developer has removed a property with the api and is trying to then set the value of that property again... - // BUT, as it turns out the content.Properties.Remove(...) method is NEVER used, because why and how could it? you never remove a property from - // a content item directly. + if (!Properties.TryGetValue(propertyTypeAlias, out var property)) + throw new InvalidOperationException($"No PropertyType exists with the supplied alias \"{propertyTypeAlias}\"."); - var propertyType = AllPropertyTypes.FirstOrDefault(x => x.Alias.InvariantEquals(propertyTypeAlias)); - if (propertyType == null) - throw new InvalidOperationException($"No PropertyType exists with the supplied alias \"{propertyTypeAlias}\"."); - - property = propertyType.CreateProperty(); - property.SetValue(value, culture, segment); - Properties.Add(property); - } + property.SetValue(value, culture, segment); //bump the culture to be flagged for updating this.TouchCulture(culture); diff --git a/src/Umbraco.Core/Models/IMedia.cs b/src/Umbraco.Core/Models/IMedia.cs index 3ad4f87aa8..75e94d66e7 100644 --- a/src/Umbraco.Core/Models/IMedia.cs +++ b/src/Umbraco.Core/Models/IMedia.cs @@ -1,9 +1,5 @@ -using Umbraco.Core.Persistence.Mappers; - -namespace Umbraco.Core.Models +namespace Umbraco.Core.Models { public interface IMedia : IContentBase - { - - } + { } } diff --git a/src/Umbraco.Core/Models/Media.cs b/src/Umbraco.Core/Models/Media.cs index d281f8f267..002611c09c 100644 --- a/src/Umbraco.Core/Models/Media.cs +++ b/src/Umbraco.Core/Models/Media.cs @@ -80,6 +80,5 @@ namespace Umbraco.Core.Models Properties.CollectionChanged -= PropertiesChanged; // be sure not to double add Properties.CollectionChanged += PropertiesChanged; } - } } diff --git a/src/Umbraco.Core/Models/MediaExtensions.cs b/src/Umbraco.Core/Models/MediaExtensions.cs index ff5e47394f..1166698adb 100644 --- a/src/Umbraco.Core/Models/MediaExtensions.cs +++ b/src/Umbraco.Core/Models/MediaExtensions.cs @@ -2,7 +2,6 @@ using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using Umbraco.Core.Composing; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; using Umbraco.Core.PropertyEditors.ValueConverters; @@ -20,8 +19,8 @@ namespace Umbraco.Core.Models return string.Empty; // TODO: would need to be adjusted to variations, when media become variants - var jsonString = property?.GetValue() as string; - if (jsonString == null) return string.Empty; + if (!(property.GetValue() is string jsonString)) + return string.Empty; if (property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.UploadField) return jsonString; diff --git a/src/Umbraco.Core/Models/PropertyCollection.cs b/src/Umbraco.Core/Models/PropertyCollection.cs index ed641080ea..977600a2f7 100644 --- a/src/Umbraco.Core/Models/PropertyCollection.cs +++ b/src/Umbraco.Core/Models/PropertyCollection.cs @@ -93,7 +93,7 @@ namespace Umbraco.Core.Models } /// - /// Adds or update a property. + /// Adds or updates a property. /// internal new void Add(Property property) { diff --git a/src/Umbraco.Core/Services/IContentTypeBaseServiceProvider.cs b/src/Umbraco.Core/Services/IContentTypeBaseServiceProvider.cs index 70327e7baf..d0146ce043 100644 --- a/src/Umbraco.Core/Services/IContentTypeBaseServiceProvider.cs +++ b/src/Umbraco.Core/Services/IContentTypeBaseServiceProvider.cs @@ -18,5 +18,10 @@ namespace Umbraco.Core.Services /// to retrieve the content / media / whatever type as . /// IContentTypeBaseService For(IContentBase contentBase); + + /// + /// Gets the content type of an object. + /// + IContentTypeComposition GetContentTypeOf(IContentBase contentBase); } } diff --git a/src/Umbraco.Core/Services/Implement/ContentTypeBaseServiceProvider.cs b/src/Umbraco.Core/Services/Implement/ContentTypeBaseServiceProvider.cs index e20ff28ab3..5a56dfe3bc 100644 --- a/src/Umbraco.Core/Services/Implement/ContentTypeBaseServiceProvider.cs +++ b/src/Umbraco.Core/Services/Implement/ContentTypeBaseServiceProvider.cs @@ -31,5 +31,12 @@ namespace Umbraco.Core.Services.Implement throw new ArgumentException($"Invalid contentBase type: {contentBase.GetType().FullName}" , nameof(contentBase)); } } + + // note: this should be a default interface method with C# 8 + public IContentTypeComposition GetContentTypeOf(IContentBase contentBase) + { + if (contentBase == null) throw new ArgumentNullException(nameof(contentBase)); + return For(contentBase)?.Get(contentBase.ContentTypeId); + } } } diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index b29977697a..f773d5d152 100755 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -291,7 +291,6 @@ - diff --git a/src/Umbraco.Tests/Testing/ContentBaseExtensions.cs b/src/Umbraco.Tests/Testing/ContentBaseExtensions.cs index 6d3d2c7683..d33818a31b 100644 --- a/src/Umbraco.Tests/Testing/ContentBaseExtensions.cs +++ b/src/Umbraco.Tests/Testing/ContentBaseExtensions.cs @@ -9,7 +9,7 @@ namespace Umbraco.Tests.Testing { public static class ContentBaseExtensions { - + /// /// Set property values by alias with an anonymous object. /// @@ -22,29 +22,12 @@ namespace Umbraco.Tests.Testing var propertyInfos = value.GetType().GetProperties(); foreach (var propertyInfo in propertyInfos) { - if (content.Properties.TryGetValue(propertyInfo.Name, out var property)) - { - property.SetValue(propertyInfo.GetValue(value, null), culture, segment); - //Update item with newly added value - content.Properties.Add(property); - } - else - { + if (!content.Properties.TryGetValue(propertyInfo.Name, out var property)) + throw new Exception($"The property alias {propertyInfo.Name} is not valid, because no PropertyType with this alias exists"); - //fixme: Can this ever happen? According to the ctor in ContentBase (EnsurePropertyTypes), all properties will be created based on the content type's property types - // so how can a property not be resolved by the alias on the content.Properties but it can on the content type? - // This maybe can happen if a developer has removed a property with the api and is trying to then set the value of that property again... - // BUT, as it turns out the content.Properties.Remove(...) method is NEVER used, because why and how could it? you never remove a property from - // a content item directly. - - var propertyType = ((ContentBase)content).AllPropertyTypes.FirstOrDefault(x => x.Alias == propertyInfo.Name); - if (propertyType == null) - throw new Exception($"The property alias {propertyInfo.Name} is not valid, because no PropertyType with this alias exists"); - //Create new Property to add to collection - property = propertyType.CreateProperty(); - property.SetValue(propertyInfo.GetValue(value, null), culture, segment); - content.Properties.Add(property); - } + property.SetValue(propertyInfo.GetValue(value, null), culture, segment); + //Update item with newly added value + content.Properties.Add(property); } } } diff --git a/src/Umbraco.Web/Editors/Filters/MemberSaveValidationAttribute.cs b/src/Umbraco.Web/Editors/Filters/MemberSaveValidationAttribute.cs index 80d5f684fe..c99bc64e23 100644 --- a/src/Umbraco.Web/Editors/Filters/MemberSaveValidationAttribute.cs +++ b/src/Umbraco.Web/Editors/Filters/MemberSaveValidationAttribute.cs @@ -1,10 +1,6 @@ -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Web.Http.Controllers; +using System.Web.Http.Controllers; using System.Web.Http.Filters; using Umbraco.Core.Logging; -using Umbraco.Core.Models; using Umbraco.Core.Services; using Umbraco.Web.Composing; using Umbraco.Web.Models.ContentEditing; @@ -20,9 +16,9 @@ namespace Umbraco.Web.Editors.Filters private readonly IUmbracoContextAccessor _umbracoContextAccessor; private readonly IMemberTypeService _memberTypeService; - public MemberSaveValidationAttribute() : this(Current.Logger, Current.UmbracoContextAccessor, Current.Services.MemberTypeService) - { - } + public MemberSaveValidationAttribute() + : this(Current.Logger, Current.UmbracoContextAccessor, Current.Services.MemberTypeService) + { } public MemberSaveValidationAttribute(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, IMemberTypeService memberTypeService) { diff --git a/src/Umbraco.Web/Editors/Filters/MemberValidationHelper.cs b/src/Umbraco.Web/Editors/Filters/MemberValidationHelper.cs index 8f98765c34..ac72019cdf 100644 --- a/src/Umbraco.Web/Editors/Filters/MemberValidationHelper.cs +++ b/src/Umbraco.Web/Editors/Filters/MemberValidationHelper.cs @@ -7,7 +7,6 @@ using System.Web.Http.Controllers; using System.Web.Http.ModelBinding; using System.Web.Security; using Umbraco.Core; -using Umbraco.Core.Composing; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Services; @@ -23,7 +22,8 @@ namespace Umbraco.Web.Editors.Filters { private readonly IMemberTypeService _memberTypeService; - public MemberValidationHelper(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, IMemberTypeService memberTypeService) : base(logger, umbracoContextAccessor) + public MemberValidationHelper(ILogger logger, IUmbracoContextAccessor umbracoContextAccessor, IMemberTypeService memberTypeService) + : base(logger, umbracoContextAccessor) { _memberTypeService = memberTypeService; }