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
This commit is contained in:
Andrea Piovanelli 2023-10-27 09:22:24 +02:00 committed by GitHub
parent a4be7c68cb
commit 03884cbd64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 9 deletions

View File

@ -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<ContentItem>(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<ContentItem>(localField.Ids, VersionOptions.Published, QueryHints.Empty));
}
}
}

View File

@ -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<IEnumerable<MediaPart>>(() => _contentManager.GetMany<MediaPart>(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<IEnumerable<MediaPart>>(() => contentItem.ContentManager.GetMany<MediaPart>(localField.Ids, VersionOptions.Published, QueryHints.Empty).ToList());
}
}
}