mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Perf: lazy-loading for ContentPart<TRecord>.Record property
Content item parts that are not referenced do not need to acquire their data --HG-- branch : dev
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using Orchard.ContentManagement.Utilities;
|
||||
|
||||
namespace Orchard.ContentManagement {
|
||||
public abstract class ContentPart : IContent {
|
||||
public virtual ContentItem ContentItem { get; set; }
|
||||
}
|
||||
|
||||
public class ContentPart<TRecord> : ContentPart {
|
||||
public TRecord Record { get; set; }
|
||||
public readonly LazyField<TRecord> _record = new LazyField<TRecord>();
|
||||
public TRecord Record { get { return _record.Value; } set { _record.Value = value; } }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,16 @@ namespace Orchard.ContentManagement.Handlers {
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
protected virtual TRecord GetRecordCore(ContentItemVersionRecord versionRecord) {
|
||||
return _repository.Get(versionRecord.ContentItemRecord.Id);
|
||||
}
|
||||
|
||||
protected virtual TRecord CreateRecordCore(ContentItemVersionRecord versionRecord, TRecord record) {
|
||||
record.ContentItemRecord = versionRecord.ContentItemRecord;
|
||||
_repository.Create(record);
|
||||
return record;
|
||||
}
|
||||
|
||||
protected override void Activated(ActivatedContentContext context, ContentPart<TRecord> instance) {
|
||||
if (instance.Record != null) {
|
||||
throw new InvalidOperationException(string.Format(
|
||||
@@ -36,23 +46,12 @@ namespace Orchard.ContentManagement.Handlers {
|
||||
}
|
||||
|
||||
protected override void Creating(CreateContentContext context, ContentPart<TRecord> instance) {
|
||||
instance.Record.ContentItemRecord = context.ContentItemRecord;
|
||||
_repository.Create(instance.Record);
|
||||
}
|
||||
|
||||
protected virtual TRecord GetRecord(LoadContentContext context) {
|
||||
return _repository.Get(context.Id);
|
||||
CreateRecordCore(context.ContentItemVersionRecord, instance.Record);
|
||||
}
|
||||
|
||||
protected override void Loading(LoadContentContext context, ContentPart<TRecord> instance) {
|
||||
var record = GetRecord(context);
|
||||
if (record != null) {
|
||||
instance.Record = record;
|
||||
}
|
||||
else {
|
||||
var createContext = new CreateContentContext(context.ContentItem);
|
||||
Creating(createContext, instance);
|
||||
}
|
||||
var versionRecord = context.ContentItemVersionRecord;
|
||||
instance._record.Loader(prior => GetRecordCore(versionRecord) ?? CreateRecordCore(versionRecord, prior));
|
||||
}
|
||||
|
||||
protected override void Versioning(VersionContentContext context, ContentPart<TRecord> existing, ContentPart<TRecord> building) {
|
||||
|
||||
@@ -7,14 +7,15 @@ namespace Orchard.ContentManagement.Handlers {
|
||||
: base(repository) {
|
||||
}
|
||||
|
||||
protected override TRecord GetRecord(LoadContentContext context) {
|
||||
return _repository.Get(context.ContentItemVersionRecord.Id);
|
||||
protected override TRecord GetRecordCore(ContentItemVersionRecord versionRecord) {
|
||||
return _repository.Get(versionRecord.Id);
|
||||
}
|
||||
|
||||
protected override void Creating(CreateContentContext context, ContentPart<TRecord> instance) {
|
||||
instance.Record.ContentItemRecord = context.ContentItemRecord;
|
||||
instance.Record.ContentItemVersionRecord = context.ContentItemVersionRecord;
|
||||
_repository.Create(instance.Record);
|
||||
protected override TRecord CreateRecordCore(ContentItemVersionRecord versionRecord, TRecord record) {
|
||||
record.ContentItemRecord = versionRecord.ContentItemRecord;
|
||||
record.ContentItemVersionRecord = versionRecord;
|
||||
_repository.Create(record);
|
||||
return record;
|
||||
}
|
||||
|
||||
protected override void Versioning(VersionContentContext context, ContentPart<TRecord> existing, ContentPart<TRecord> building) {
|
||||
|
||||
@@ -152,6 +152,7 @@
|
||||
<Compile Include="Caching\Weak.cs" />
|
||||
<Compile Include="ContentManagement\DefaultContentManagerSession.cs" />
|
||||
<Compile Include="ContentManagement\IContentManagerSession.cs" />
|
||||
<Compile Include="ContentManagement\Utilities\LazyField.cs" />
|
||||
<Compile Include="FileSystems\WebSite\WebSiteFolder.cs" />
|
||||
<Compile Include="FileSystems\AppData\IAppDataFolder.cs" />
|
||||
<Compile Include="FileSystems\WebSite\IWebSiteFolder.cs" />
|
||||
|
||||
Reference in New Issue
Block a user