Copied to clipboard

Flag this post as spam?

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


  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 02, 2023 @ 12:08
    Chriztian Steinmeier
    0

    Getting an UmbracoHelper in a custom class

    Hi there - super contrived example here, but I have this problem where I have a Helpers file with some methods I've collected through years of Umbraco experience.

    So in a Template or a Partial, I can do stuff like this:

    @using Vintage;
    
    <p>@Helpers.DoStuff()</p>
    

    My problem is that now with .net Core I don't know how to do this thing that used to be fairly simple (using UmbracoHelper). I'm aware that I need to use Dependency Injection somehow, but I just can't wrap my head around how much ceremony is needed for it to work, so here's a very thin reduction of my problem:

    namespace Vintage;
    
    public class Helpers {
    
        public static GetProductById(int id) {
            return umbracoHelper.Content(id)
        }
    }
    

    What would I need to do, to get a reference to an UmbracoHelper (or whatever its equivalent is in Umbraco 10) in the above example?

    Thanks!

    /Chriztian

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Mar 02, 2023 @ 13:45
    Sebastiaan Janssen
    1

    You'll hopefully be very pleased at this tiny ceremony:

    @using Vintage;
    @inject Umbraco.Cms.Web.Common.UmbracoHelper Helpers
    
    <p>@Helpers.DoStuff()</p>
    
  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Mar 02, 2023 @ 14:07
    Sebastiaan Janssen
    1

    I may have misunderstood your question though, in a C# class the ceremony (also known as constructor injection) looks a little like:

    using Umbraco.Cms.Web.Common;
    
    namespace Vintage;
    
    public class Helpers {
    
        private readonly UmbracoHelper _umbracoHelper;
    
        public Helpers(UmbracoHelper umbracoHelper) {
            _umbracoHelper = umbracoHelper;
        }
    
        public static GetProductById(int id) {
            return _umbracoHelper.Content(id)
        }
    }
    

    This gets super "fun" when you eventually stumble upon a No parameterless constructor defined error. That's something we can talk about if and when that happens though! πŸ˜…

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 02, 2023 @ 16:18
    Chriztian Steinmeier
    0

    Thanks Sebastiaan;

    Yeah, this puts me at β€œAn object reference is required for the non-static field, method, or property Helpers._umbracoHelper...”

    /Chriztian

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 02, 2023 @ 14:15
    Dave Woestenborghs
    100

    Or if you no able to use DI you can always use the static service provider to get a instance.

    namespace Vintage;
    
    public static class Helpers {
    
        public static IPublishedContent? GetProductById(int id)
            {
                var accessor = StaticServiceProvider.Instance.GetRequiredService<IUmbracoHelperAccessor>();
    
                if (accessor.TryGetUmbracoHelper(out var umbracoHelper))
                {
                    return umbracoHelper.Content(id);
                }
    
                return null;
            }
    }
    

    Dave

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 02, 2023 @ 16:20
    Chriztian Steinmeier
    0

    Thanks Dave!

    I got it working with this after some fiddling (namespaces...).

    If I understand the (source)code comments correctly this is the absolute last resort method, but thanks again - it works!! :D

    /Chriztian

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 02, 2023 @ 16:22
    Dave Woestenborghs
    1

    Hi Chriztian,

    I know it is a last resort and that DI is preferred. But we are in the middle of upgrading a V8 site to V10 that makes uses of Current a lot. So we are using this as well before we do a proper refactor.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft