Copied to clipboard

Flag this post as spam?

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


  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Mar 06, 2015 @ 10:35
    Warren Buckley
    0

    Querying - UmbracoContext.ContentCache.GetByXPath OR Umbraco.TypedContentAtXPath ?

    Hello all
    I am currently rewriting the uHangout website and want to ensure I am querying for nodes in the most efficient manner.

    Firstly lets take two examples, I am curious do they do the same thing under the hood and just use different syntax or if one is different than the other and effectively more peformant than the other.

    This example code is a WebAPI class that inherits from UmbracoApiController, so Umbraco Services, UmbracoContext & UmbracoHelper are all available to me.

    //Get me all the nodes of doctype 'Video'
    var videos = Umbraco.TypedContentAtXPath("//Video");
    var videos2 = UmbracoContext.ContentCache.GetByXPath("//Video");
    
    //Find a video by a YouTube ID on a property
    var findVideo = videos.SingleOrDefault(x => x.GetPropertyValue<string>("youTubeVideoId") == videoIdToFind);
    

    So come on you clever lot tell me which one is the best one to use and if there is any difference between the two at all?

    Also I am curious to know if there is a nicer syntax way similar in the ContentService (I know not designed for read but CRUD) there is a method of Services.ContentService.GetContentOfContentType() which is what I am replicating with the the XPath example above and then applying Lambada Linq expressions to filter, order & take what I need.

    Cheers,
    Warren

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Mar 06, 2015 @ 12:44
    Jeroen Breuer
    1

    Hi Warren,

    I've used both before and too be honest I don't know the real difference either. Both also have methods to return content based on an id. The only difference I know of is that both are available at different parts of the code. 

    Umbraco.TypedContentAtXPath --> I usually use in views or controllers.
    contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByXPath or umbracoContext.ContentCache.GetByXPath  --> I use in content finders or url providers. 

    Have you already looked at the source code? Maybe both go down to the same method under the hood.

    Jeroen

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Mar 06, 2015 @ 12:45
    Matt Brailsford
    102

    Hey Warren,

    There is no difference. If you look in the Umbraco source and trace back the method calls, Umbraco.TypedContentAtXPath ultimately calls UmbracoContext.ContentCache.GetByXPath. Umbraco.TypedContentAtXPath then is just on the UmbracoHelper class for convenience / a nicer way to get to it.

    Matt

  • Mads Krohn 211 posts 504 karma points c-trib
    Mar 06, 2015 @ 12:52
    Mads Krohn
    1

    Hi Warren

    As far as I can tell through a source-code-deep-dive they both eventually end up in PublishedContentCache.GetByXPath(), so performance should be exactly the same. I guess your desired way there depends on your current context. When you have both available, just pick one I guess :)

    If you want to bypass the XPath route, you should probably look to Examine.

    BaseSearchProvider searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
    ISearchCriteria criteria = searcher
        .CreateSearchCriteria("content")
        .NodeTypeAlias("Video")
        .And()
        .Field("youTubeVideoId", videoIdToFind)
        .Compile();
    IEnumerable<IPublishedContent> results = Umbraco.TypedSearch(criteria, searcher);
    

    Note that the TypedSearch method is on the UmbracoHelper instance.
    I'm not sure you'll find a similar syntax as the one from the ContentService, as least not at the moment.
    But Shannon would know more about that than me.

    - Cheers

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Mar 06, 2015 @ 13:03
    Warren Buckley
    0

    Hi Mads,
    I know my colleague Mr Examine aka Ismail Mayat would prefer me to build everything using Examine & Lucene :-P

    So many different ways to query, I always get overwhelmed and I just want to make the new uHangout site best practise for me to learn anything new along the way. As I am always so resilient to use Examine, maybe its time I should.

    Cheers all
    Warren

  • Mads Krohn 211 posts 504 karma points c-trib
    Mar 06, 2015 @ 13:25
    Mads Krohn
    0

    Hi Warren

    I didn't know that you and Ismail are colleagues, that makes using Examine even more obvious :)
    But indeed, the options are many, too many some would say. I guess that's the consequence of putting in so many context based helper methods, because, most of the time, they all end up doing the same thing anyways :)

    Cheers

Please Sign in or register to post replies

Write your reply to:

Draft