Copied to clipboard

Flag this post as spam?

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


  • paulthorpe 10 posts 193 karma points
    Jul 25, 2017 @ 14:07
    paulthorpe
    0

    I have been reading through the Anti-Patterns document found at https://our.umbraco.org/documentation/Reference/Common-Pitfalls/ and I am a little confused with first item "Usage of Singletons and Statics".

    I have a controller that calls out to a class that is injected using AutoFac, This class needs to make use of the Umbraco APIs specifically TypedContentAtRoot()

    Within My class I have carried out the following

    UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
    var MyRootItem = umbracoHelper.TypedContentAtRoot();
    

    My question is should I be creating the UmbracoHelper in this way as based on the Common Pitfalls document it states don't use UmbracoContext.Current

    Am I missing something How should I create the UmbracoHelper if I shouldn't use UmbracoContext.Current .

    I have looked around the web and there are a number discussions but I'm still not clear so any information would be really useful.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jul 25, 2017 @ 14:54
    Ismail Mayat
    101

    Paul,

    In the past we have also injected umbracohelper in using IOC in this instance it was ninject so:

     var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    

    then

     kernel.Bind<IUmbracoService>()
     .ToConstructor(ctorArg => new UmbracoService(contentService, umbracoHelper, loggingService))
                .InRequestScope();
    

    The IUmbracoService is our own wrapper that makes use of umbraco helper to get nodes in same way you are eg TypeContentAtRoot()

    Regards

    Ismail

  • Harry Spyrou 212 posts 604 karma points
    Jun 05, 2018 @ 10:26
    Harry Spyrou
    0

    Hello,

    I was going to start a new thread but this one is what I need. I've been reading the Anti patterns and pitfalls, too. I came across this line:

    private static _request = HttpContext.Current.Request;
    

    In one of the websites I developed I have this:

    string path = System.Web.HttpContext.Current.Request.Path.ToLower();
    

    If I understand correctly, this is fine as long as it's not static?

Please Sign in or register to post replies

Write your reply to:

Draft