UmbracoHelper in function - Instantiate or pass as parameter?
I have a function that is using UmbracoHelper - it is in a class library and it's going to be used in foreach loops about 50% of the time.
A simplified version is as follows:
public static IEnumerable<IPublishedContent> GetPickerItems(this IPublishedContent item, string pickerAlias)
{
Umbraco.Web.UmbracoHelper u = new Umbraco.Web.UmbracoHelper(UmbracoContext.Current);
if (!item.HasValue(pickerAlias)) { yield break; }
foreach (string pageId in item.GetPropertyValue<string>(pickerAlias, "").Split(','))
{
yield return u.TypedContent(pageId);
}
}
I am wondering about the following:
Is it right to use UmbracoHelper this way? The function works, but is there something I'm missing regarding the scope of UmbracoContext.Current?
Should I instantiate a new UmbracoHelper in every call (as I'm doing right now) or pass one as a parameter? I examined the code for UmbracoHelper and saw that it's a lightweight object - most calls do lazy instantiation, so I don't think it's going to have a big performance/memory hit as it is now, but again, is there something I'm missing?
UmbracoHelper in function - Instantiate or pass as parameter?
I have a function that is using UmbracoHelper - it is in a class library and it's going to be used in foreach loops about 50% of the time.
A simplified version is as follows:
I am wondering about the following:
Is it right to use UmbracoHelper this way? The function works, but is there something I'm missing regarding the scope of UmbracoContext.Current?
Should I instantiate a new UmbracoHelper in every call (as I'm doing right now) or pass one as a parameter? I examined the code for UmbracoHelper and saw that it's a lightweight object - most calls do lazy instantiation, so I don't think it's going to have a big performance/memory hit as it is now, but again, is there something I'm missing?
I created a function that stores it in the HTTP context: https://github.com/rhythmagency/rhythm.umbraco.extensions/blob/56a328f8c84d4f155d7201494be62ecfb0aaf32e/trunk/Rhythm.Extensions/Rhythm.Extensions/Helpers/ContentHelper.cs#L27
That way, only one gets created per request.
Essentially, a singleton. This was my third option, but I was afraid to ask :)
Thanks!
is working on a reply...