Copied to clipboard

Flag this post as spam?

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


  • Mimi 21 posts 91 karma points
    Jul 04, 2022 @ 08:54
    Mimi
    0

    Getting Current.Factory.GetInstance in v9?

    Hi all, hope someone can help me with this.

    I have the following code which does not work any more in umbraco v.9:

     return Current.Factory.TryGetInstance<T>();
    

    How can I use Current.Factory.GetInstance in Umbraco v.9 ?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jul 04, 2022 @ 09:29
    Dan Diplo
    2

    The short answer is: You shouldn't really be doing that.

    You should be injecting dependencies you need. You can usually do this in the constructor of the class you want to "depend" on your instance, but you can also do it in Views too. See https://our.umbraco.com/documentation/reference/using-ioc/

    To inject a dependency in a view you can do:

    @inject IFooBar fooBar

    See https://docs.microsoft.com/en-us/aspnet/core/mvc/views/dependency-injection?view=aspnetcore-6.0

    You can also inject dependencies in View Components, which can replace traditional partials - see https://our.umbraco.com/Documentation/Reference/Templating/Mvc/ViewComponents

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Jul 04, 2022 @ 11:27
    Marc Goodson
    3

    Hi @Fa

    As Dan says you should be injecting all the things...

    ... but ... if you really really really couldn't inject your dependency in a particular scenario, then there is an equivalent in V9

    Umbraco.Cms.Web.Common.DependencyInjection.StaticServiceProvider

    which allows you to locate services registered in the Dependency Injection container, that you just can't access in your context via actual injection.

    eg

    StaticServiceProvider.Instance.GetRequiredService<IVariationContextAccessor>();
    

    but don't tell anyone I told you :-P

    regards

    Marc

  • Mimi 21 posts 91 karma points
    Jul 13, 2022 @ 08:10
    Mimi
    0

    Thanks Marc Goodson and Dan Diplo. I did go with injecting all the things and it is working fine. :)

Please Sign in or register to post replies

Write your reply to:

Draft