From b1f0e002879efaedee034b2f230ee0a7f0a653c8 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Mon, 10 Dec 2012 03:40:50 +0500 Subject: [PATCH] Adds performance benefits for PublishedContentBase --- .../Models/PublishedContentBase.cs | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Web/Models/PublishedContentBase.cs b/src/Umbraco.Web/Models/PublishedContentBase.cs index 54c787db7f..8ce6f65ac4 100644 --- a/src/Umbraco.Web/Models/PublishedContentBase.cs +++ b/src/Umbraco.Web/Models/PublishedContentBase.cs @@ -16,6 +16,9 @@ namespace Umbraco.Web.Models /// public abstract class PublishedContentBase : IPublishedContent { + private string _url; + private readonly Dictionary _resolvePropertyValues = new Dictionary(); + /// /// Returns the Url for this content item /// @@ -27,6 +30,9 @@ namespace Umbraco.Web.Models { get { + if (_url != null) + return _url; + switch (ItemType) { case PublishedItemType.Content: @@ -34,15 +40,18 @@ namespace Umbraco.Web.Models throw new InvalidOperationException("Cannot resolve a Url for a content item with a null UmbracoContext.Current reference"); if (UmbracoContext.Current.NiceUrlProvider == null) throw new InvalidOperationException("Cannot resolve a Url for a content item with a null UmbracoContext.Current.NiceUrlProvider reference"); - return UmbracoContext.Current.NiceUrlProvider.GetNiceUrl(this.Id); + _url= UmbracoContext.Current.NiceUrlProvider.GetNiceUrl(this.Id); + break; case PublishedItemType.Media: var prop = GetProperty("umbracoFile"); if (prop == null) throw new NotSupportedException("Cannot retreive a Url for a media item if there is no 'umbracoFile' property defined"); - return prop.Value.ToString(); + _url = prop.Value.ToString(); + break; default: throw new ArgumentOutOfRangeException(); } + return _url; } } @@ -75,7 +84,15 @@ namespace Umbraco.Web.Models /// public virtual object this[string propertyAlias] { - get { return this.GetPropertyValue(propertyAlias); } + get + { + //check this instance's cache, this is better for performance because resolving a value can + //have performance impacts since it has to resolve Urls and IPropertyEditorValueConverter's as well. + if (_resolvePropertyValues.ContainsKey(propertyAlias)) + return _resolvePropertyValues[propertyAlias]; + _resolvePropertyValues.Add(propertyAlias, this.GetPropertyValue(propertyAlias)); + return _resolvePropertyValues[propertyAlias]; + } } public abstract IPublishedContentProperty GetProperty(string alias);