Copied to clipboard

Flag this post as spam?

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


  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 06, 2013 @ 09:57
    Dan Diplo
    1

    Instantiating UmbracoHelper in Static Class Library

    This question arises out of a question I asked on Twitter:

    "Is using UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current) in a static method (called from web) OK?"

    When I say "called from web" I'm refering to calling it from within Umbraco (ie. from a view or maybe a UserControl). I do NOT mean calling it form outside of Umbraco, where obviously the UmbracoContext wouldn't exist.

    The main reason I find myself needing access to UmbracoHelper is to create new TypedContent based on a node id.

    To provide a concrete example, here is an extension method I created to get back IPublishedContent items for a multi-node tree picker (set to return CSV):

    public static List<IPublishedContent> GetContentFromMultiNodePicker(this IPublishedContent content, string alias, bool recursive)
    {
        List<IPublishedContent> list = new List<IPublishedContent>();
    
        string data = content.GetPropertyValue<string>(alias, recursive, null);
    
        if (!String.IsNullOrEmpty(data))
        {
            var ids = data.Split(',');
    
            UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
    
            foreach (string id in ids)
            {
                var node = helper.TypedContent(id);
    
                if (node != null)
                {
                    list.Add(node);
                }
            }
        }
    
        return list;
    }
    
    The only way I can find to get back actual nodes (IPublishedContent) is via the TypedContent method of the UmbracoHelper instance.
    I find there are many other places where I similary need to do this - mainly when I have a library that accepts a node by Id (as an int) but needs to convert it into IPublishedContent. I want to abstract common code like this into reusable methods.
    Currently this works fine - I've had no issues. But I'm aware that Shannon seems to think this is a bad idea, which worries me :) So, what is the verdict?
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 06, 2013 @ 10:33
    Marc Goodson
    1

    Hi Dan

    What I usually do here, is pass the current UmbracoContext into the method to use the 'typedness'

    but that's if I'm creating a razor helper in app_code

    eg:

    @helper DisplayImage(IPublishedContent content, UmbracoHelper umbracoHelper, string mobileImageAlias, string largeImageAlias)

    and then I use it in the method;

    umbracoHelper.TypedContentAtPath("//thing")....

    If the project is being built with visual studio, then I often create further extension methods onto UmbracoHelper for my custom methods...

    eg

    public static class MyUmbracoHelpers

    {

    public static IEnumerable<IPublishedContent> GetAllThings(this UmbracoHelper umbraco, string someParameter){

    // logic to get and return things

    }

    }

    then you can just call it as @Umbraco.GetAllThings("parameterthing")

    but in your example above you probably want to look at a PropertyEditorValueConverter,

    there is one for the multinode tree picker in this package;

    http://our.umbraco.org/projects/developer-tools/umbraco-core-property-editor-converters

     

    With the MultinodeTreePickerPropertyEditorValueConverter in your project, instead of using your helper above you'd be able to write

    var allPickedNodes = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("myPickerPropertyAlias")

    if that makes sense ?

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 06, 2013 @ 12:05
    Dan Diplo
    0

    Hi Marc,

    Cheers for info. I did intially pass in the UmbracoHelper instance to the method, but it seemed a bit "clumsy". Often a lot of the methods I write are consumed by front-end guys so I try and make the method signatures are simple as possible. As instantiating the UmbracoHelper in the method worked I thought I might as well remove one (seemingly) uneeded parameter.

    I thought someone might mention property editor converters :) Yeah, I realise they are the best way of achieveing what I was doing, but I was more interested in the wider point of whether it was wrong or not to create a helper instance within a method.

    Would love to here the views of the core team?

     

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 06, 2013 @ 12:10
    Marc Goodson
    0

    If the frontend people don't need to edit the logic of the function,

    then extension methods to the UmbracoHelper class, look most neat / magical :-)

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 06, 2013 @ 12:45
    Dan Diplo
    0

    Making it an extension of the UmbracoHelper class is a really good idea - I like it. I'm going to do that :)

    Interestingly I looked at the source of the MultiNodeTreePickerPropertyEditorValueConverter control and it had this in it...

    var umbHelper = new UmbracoHelper(UmbracoContext.Current);

    So...!

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Nov 06, 2013 @ 15:46
    Andy Butland
    1

    I was looking into this the other day... there was a very long thread on the subject.  Maybe things have moved on since then, but at the time Shannon was commenting that the new UmbracoHelper(UmbracoContext.Current) is the way to go (see page 5).

Please Sign in or register to post replies

Write your reply to:

Draft