Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sotiris Filippidis 286 posts 1501 karma points
    May 20, 2016 @ 23:09
    Sotiris Filippidis
    0

    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:

    1. Is it right to use UmbracoHelper this way? The function works, but is there something I'm missing regarding the scope of UmbracoContext.Current?

    2. 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?

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    May 20, 2016 @ 23:29
  • Sotiris Filippidis 286 posts 1501 karma points
    May 20, 2016 @ 23:32
    Sotiris Filippidis
    0

    Essentially, a singleton. This was my third option, but I was afraid to ask :)

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft