Copied to clipboard

Flag this post as spam?

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


  • Perry Cope 31 posts 195 karma points
    Dec 20, 2022 @ 16:07
    Perry Cope
    0

    Getting An Umbraco service in a UmbracoHelper Extension

    Before in Umbraco 8 i was able to get a service inside a static function for example a UmbracoHelper Extension like so

    IDataTypeService dataTypeService = Umbraco.Core.Composing.Current.Services.DataTypeService;
    

    Am i still able to do this in Umbraco 9+ i cant seem to find anything in the Docs everything pushes you towards using dependancy injection which i cant in the Static Umbraco Helper.

    I assume the alternative is to make it a service instead and inject that in to the view but im porting a large site from Umbraco 8 to 11, keeping it as UmbracoHelper function would reduce code changes

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Dec 21, 2022 @ 07:20
    Søren Kottal
    100

    Hi Perry

    You probably shouldn't do it, but there is a static service provider you can use. It's described here: https://www.andybutland.dev/2022/03/service-location-in-umbraco-9.html

  • Luuk Peters 85 posts 330 karma points
    Dec 21, 2022 @ 12:41
    Luuk Peters
    1

    I understand that a migration to a new version is a lot of work and it makes sense to not touch every bit of code and rewrite everything. But at least place it on a backlog to rewrite the helper into a service instead of a static class.

    Although the service locator should work, it's not the prefered approach.

Please Sign in or register to post replies

Write your reply to:

Draft