From 03884cbd6431721ffe69b03c69ba672a1cb44dc1 Mon Sep 17 00:00:00 2001 From: Andrea Piovanelli <83577153+AndreaPiovanelliLaser@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:22:24 +0200 Subject: [PATCH] Lazy field loading using content item ContentManager instead of the injected one. (#8725) * Lazy field loading using content item ContentManager instead of the injected one. * Removed unused _contentManager variable --- .../Handlers/ContentPickerFieldHandler.cs | 6 ++---- .../Handlers/MediaLibraryPickerFieldHandler.cs | 7 ++----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentPickerFieldHandler.cs b/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentPickerFieldHandler.cs index 74ea52acb..d11d0d99b 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentPickerFieldHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentPicker/Handlers/ContentPickerFieldHandler.cs @@ -6,14 +6,11 @@ using Orchard.ContentPicker.Fields; namespace Orchard.ContentPicker.Handlers { public class ContentPickerFieldHandler : ContentHandler { - private readonly IContentManager _contentManager; private readonly IContentDefinitionManager _contentDefinitionManager; public ContentPickerFieldHandler( - IContentManager contentManager, IContentDefinitionManager contentDefinitionManager) { - _contentManager = contentManager; _contentDefinitionManager = contentDefinitionManager; } @@ -30,7 +27,8 @@ namespace Orchard.ContentPicker.Handlers { foreach (var field in fields) { var localField = field; - field._contentItems.Loader(() => _contentManager.GetMany(localField.Ids, VersionOptions.Published, QueryHints.Empty)); + // Using context content item's ContentManager instead of injected one to avoid lifetime scope exceptions in case of LazyFields. + field._contentItems.Loader(() => context.ContentItem.ContentManager.GetMany(localField.Ids, VersionOptions.Published, QueryHints.Empty)); } } } diff --git a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Handlers/MediaLibraryPickerFieldHandler.cs b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Handlers/MediaLibraryPickerFieldHandler.cs index 8f29e137b..06120947b 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaLibrary/Handlers/MediaLibraryPickerFieldHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.MediaLibrary/Handlers/MediaLibraryPickerFieldHandler.cs @@ -9,16 +9,12 @@ using Orchard.MediaLibrary.Models; namespace Orchard.MediaLibrary.Handlers { public class MediaLibraryPickerFieldHandler : ContentHandler { - private readonly IContentManager _contentManager; private readonly IContentDefinitionManager _contentDefinitionManager; public MediaLibraryPickerFieldHandler( - IContentManager contentManager, IContentDefinitionManager contentDefinitionManager) { - _contentManager = contentManager; _contentDefinitionManager = contentDefinitionManager; - } protected override void Loaded(LoadContentContext context) { @@ -37,7 +33,8 @@ namespace Orchard.MediaLibrary.Handlers { foreach (var field in fields) { var localField = field; - localField._contentItems = new Lazy>(() => _contentManager.GetMany(localField.Ids, VersionOptions.Published, QueryHints.Empty).ToList()); + // Using context content item's ContentManager instead of injected one to avoid lifetime scope exceptions in case of LazyFields. + localField._contentItems = new Lazy>(() => contentItem.ContentManager.GetMany(localField.Ids, VersionOptions.Published, QueryHints.Empty).ToList()); } } }