diff --git a/src/OrchardCore.Modules/OrchardCore.Title/Model/TitlePart.cs b/src/OrchardCore.Modules/OrchardCore.Title/Model/TitlePart.cs index 0b8c0d33f63..075408a9b14 100644 --- a/src/OrchardCore.Modules/OrchardCore.Title/Model/TitlePart.cs +++ b/src/OrchardCore.Modules/OrchardCore.Title/Model/TitlePart.cs @@ -1,4 +1,4 @@ -using OrchardCore.ContentManagement; +using OrchardCore.ContentManagement; using System.ComponentModel.DataAnnotations; namespace OrchardCore.Title.Model diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentElement.cs b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentElement.cs index 5144b1f8c0e..9087fb110bc 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentElement.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentElement.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -9,6 +10,8 @@ namespace OrchardCore.ContentManagement /// public class ContentElement : IContent { + internal Dictionary _elements = new Dictionary(); + protected ContentElement() : this(new JObject()) { } @@ -33,8 +36,7 @@ protected ContentElement(JObject data) /// The name of the property to look for. public bool Has(string name) { - JToken value; - return Data.TryGetValue(name, out value); + return Data.ContainsKey(name); } } } \ No newline at end of file diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentExtensions.cs b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentExtensions.cs index 4a3e0368b79..a5fdba7e3b5 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentExtensions.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentExtensions.cs @@ -41,6 +41,11 @@ public static bool Has(this ContentElement contentElement) where TElem /// The content element instance or null if it doesn't exist. public static ContentElement Get(this ContentElement contentElement, Type contentElementType, string name) { + if (contentElement._elements.TryGetValue(name, out var element)) + { + return element; + } + var elementData = contentElement.Data[name] as JObject; if (elementData == null) @@ -52,6 +57,8 @@ public static ContentElement Get(this ContentElement contentElement, Type conten result.Data = elementData; result.ContentItem = contentElement.ContentItem; + contentElement._elements[name] = result; + return result; } @@ -67,10 +74,11 @@ public static ContentElement Get(this ContentElement contentElement, Type conten if (existing == null) { - existing = new TElement(); - existing.ContentItem = contentElement.ContentItem; - contentElement.Data[name] = existing.Data; - return existing; + var newElement = new TElement(); + newElement.ContentItem = contentElement.ContentItem; + contentElement.Data[name] = newElement.Data; + contentElement._elements[name] = newElement; + return newElement; } return existing; @@ -91,6 +99,7 @@ public static ContentElement Weld(this ContentElement contentElement, string nam element.ContentItem = contentElement.ContentItem; contentElement.Data[name] = element.Data; + contentElement._elements[name] = element; } return contentElement; diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Handlers/ContentPartHandler.cs b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Handlers/ContentPartHandler.cs index 2bfe4edccb4..fdeaf46dc43 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Handlers/ContentPartHandler.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Handlers/ContentPartHandler.cs @@ -4,156 +4,137 @@ namespace OrchardCore.ContentManagement.Handlers { public abstract class ContentPartHandler : IContentPartHandler where TPart : ContentPart, new() { - async Task IContentPartHandler.ActivatedAsync(ActivatedContentContext context, ContentPart part) + Task IContentPartHandler.ActivatedAsync(ActivatedContentContext context, ContentPart part) { - if (part is TPart) - { - await ActivatedAsync(context, (TPart)part); - } + return part is TPart tpart + ? ActivatedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.ActivatingAsync(ActivatingContentContext context, ContentPart part) + Task IContentPartHandler.ActivatingAsync(ActivatingContentContext context, ContentPart part) { - if (part is TPart) - { - await ActivatingAsync(context, (TPart)part); - } + return part is TPart tpart + ? ActivatingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.InitializingAsync(InitializingContentContext context, ContentPart part) + Task IContentPartHandler.InitializingAsync(InitializingContentContext context, ContentPart part) { - if (part is TPart) - { - await InitializingAsync(context, (TPart)part); - } + return part is TPart tpart + ? InitializingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.InitializedAsync(InitializingContentContext context, ContentPart part) + Task IContentPartHandler.InitializedAsync(InitializingContentContext context, ContentPart part) { - if (part is TPart) - { - await InitializedAsync(context, (TPart)part); - } + return part is TPart tpart + ? InitializedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.CreatingAsync(CreateContentContext context, ContentPart part) + Task IContentPartHandler.CreatingAsync(CreateContentContext context, ContentPart part) { - if (part is TPart) - { - await CreatingAsync(context, (TPart)part); - } + return part is TPart tpart + ? CreatingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.CreatedAsync(CreateContentContext context, ContentPart part) + Task IContentPartHandler.CreatedAsync(CreateContentContext context, ContentPart part) { - if (part is TPart) - { - await CreatedAsync(context, (TPart)part); - } + return part is TPart tpart + ? CreatedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.LoadingAsync(LoadContentContext context, ContentPart part) + Task IContentPartHandler.LoadingAsync(LoadContentContext context, ContentPart part) { - if (part is TPart) - { - await LoadingAsync(context, (TPart)part); - } + return part is TPart tpart + ? LoadingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.LoadedAsync(LoadContentContext context, ContentPart part) + Task IContentPartHandler.LoadedAsync(LoadContentContext context, ContentPart part) { - if (part is TPart) - { - await LoadedAsync(context, (TPart)part); - } + return part is TPart tpart + ? LoadedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.UpdatingAsync(UpdateContentContext context, ContentPart part) + Task IContentPartHandler.UpdatingAsync(UpdateContentContext context, ContentPart part) { - if (part is TPart) - { - await UpdatingAsync(context, (TPart)part); - } + return part is TPart tpart + ? UpdatingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.UpdatedAsync(UpdateContentContext context, ContentPart part) + Task IContentPartHandler.UpdatedAsync(UpdateContentContext context, ContentPart part) { - if (part is TPart) - { - await UpdatedAsync(context, (TPart)part); - } + return part is TPart tpart + ? UpdatedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.VersioningAsync(VersionContentContext context, ContentPart existing, ContentPart building) + Task IContentPartHandler.VersioningAsync(VersionContentContext context, ContentPart existing, ContentPart building) { - if (existing is TPart && building is TPart) - { - await VersioningAsync(context, (TPart)existing, (TPart)building); - } + return existing is TPart texisting && building is TPart tbuilding + ? VersioningAsync(context, texisting, tbuilding) + : Task.CompletedTask; } - async Task IContentPartHandler.VersionedAsync(VersionContentContext context, ContentPart existing, ContentPart building) + Task IContentPartHandler.VersionedAsync(VersionContentContext context, ContentPart existing, ContentPart building) { - if (existing is TPart && building is TPart) - { - await VersionedAsync(context, (TPart)existing, (TPart)building); - } + return existing is TPart texisting && building is TPart tbuilding + ? VersionedAsync(context, texisting, tbuilding) + : Task.CompletedTask; } - async Task IContentPartHandler.PublishingAsync(PublishContentContext context, ContentPart part) + Task IContentPartHandler.PublishingAsync(PublishContentContext context, ContentPart part) { - if (part is TPart) - { - await PublishingAsync(context, (TPart)part); - } + return part is TPart tpart + ? PublishingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.PublishedAsync(PublishContentContext context, ContentPart part) + Task IContentPartHandler.PublishedAsync(PublishContentContext context, ContentPart part) { - if (part is TPart) - { - await PublishedAsync(context, (TPart)part); - } + return part is TPart tpart + ? PublishedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.UnpublishingAsync(PublishContentContext context, ContentPart part) + Task IContentPartHandler.UnpublishingAsync(PublishContentContext context, ContentPart part) { - if (part is TPart) - { - await UnpublishingAsync(context, (TPart)part); - } + return part is TPart tpart + ? UnpublishingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.UnpublishedAsync(PublishContentContext context, ContentPart part) + Task IContentPartHandler.UnpublishedAsync(PublishContentContext context, ContentPart part) { - if (part is TPart) - { - await UnpublishedAsync(context, (TPart)part); - } + return part is TPart tpart + ? UnpublishedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.RemovingAsync(RemoveContentContext context, ContentPart part) + Task IContentPartHandler.RemovingAsync(RemoveContentContext context, ContentPart part) { - if (part is TPart) - { - await RemovingAsync(context, (TPart)part); - } + return part is TPart tpart + ? RemovingAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.RemovedAsync(RemoveContentContext context, ContentPart part) + Task IContentPartHandler.RemovedAsync(RemoveContentContext context, ContentPart part) { - if (part is TPart) - { - await RemovedAsync(context, (TPart)part); - } + return part is TPart tpart + ? RemovedAsync(context, tpart) + : Task.CompletedTask; } - async Task IContentPartHandler.GetContentItemAspectAsync(ContentItemAspectContext context, ContentPart part) + Task IContentPartHandler.GetContentItemAspectAsync(ContentItemAspectContext context, ContentPart part) { - if (part is TPart) - { - await GetContentItemAspectAsync(context, (TPart)part); - } + return part is TPart tpart + ? GetContentItemAspectAsync(context, tpart) + : Task.CompletedTask; } public virtual Task ActivatedAsync(ActivatedContentContext context, TPart instance) => Task.CompletedTask; diff --git a/src/OrchardCore/OrchardCore.ContentManagement/Handlers/ContentPartHandlerCoordinator.cs b/src/OrchardCore/OrchardCore.ContentManagement/Handlers/ContentPartHandlerCoordinator.cs index 289d2a10eb2..a052410316e 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement/Handlers/ContentPartHandlerCoordinator.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement/Handlers/ContentPartHandlerCoordinator.cs @@ -167,11 +167,14 @@ public override async Task LoadingAsync(LoadContentContext context) foreach (var typePartDefinition in contentTypeDefinition.Parts) { var partName = typePartDefinition.PartDefinition.Name; - var part = context.ContentItem.Get(typePartDefinition.Name); + var activator = _contentPartFactory.GetTypeActivator(partName); + + var part = context.ContentItem.Get(activator.Type, typePartDefinition.Name) as ContentPart; + // If no existing part was not found in the content item, create a new one if (part == null) { - part = _contentPartFactory.GetTypeActivator(partName).CreateInstance(); + part = activator.CreateInstance(); context.ContentItem.Weld(typePartDefinition.Name, part); } @@ -183,8 +186,8 @@ public override async Task LoadingAsync(LoadContentContext context) if (!part.Has(fieldName)) { - var activator = _contentFieldFactory.GetTypeActivator(partFieldDefinition.FieldDefinition.Name); - context.ContentItem.Get(typePartDefinition.Name).Weld(fieldName, activator.CreateInstance()); + var fieldActivator = _contentFieldFactory.GetTypeActivator(partFieldDefinition.FieldDefinition.Name); + context.ContentItem.Get(typePartDefinition.Name).Weld(fieldName, fieldActivator.CreateInstance()); } } }