Copied to clipboard

Flag this post as spam?

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


  • Paul Seal 380 posts 2074 karma points MVP 2x c-trib
    Sep 06, 2016 @ 06:53
    Paul Seal
    0

    Site navigation model example with caching

    Hi all I've been working on my site and found a good way to put the site structure into a model and render it from that. Also I use caching to store that model to make the page load faster.

    I wrote a blog post about it. Take a look and let me know what you think, or if there is anything you would add.

    http://www.codeshare.co.uk/blog/umbraco-site-navigation-menu-model-example-in-c-mvc/

    Cheers

    Paul

  • Dave Woestenborghs 3205 posts 10708 karma points MVP 4x admin c-trib
    Sep 06, 2016 @ 07:37
    Dave Woestenborghs
    1

    Hi Paul,

    I just read the article and here are some things that I would have done differently. That's doesn't mean it's a better :-)

    Use MVC convetions for viewpath

    I would have created the view in a folder ~/Views/SiteLayout/. Then you can do and MVC will pick up the path because the view is located in a folder with the name of the controller

    return PartialView("_TopNavigation.cshtml", nav);
    

    Query content instead of using path level

    You use a fixed level for your homepage and then use the path to determine it. I would have just queried the content to get the homepage.

    IPublishedContent homePage = this.Umbraco.AssignedContentItem.AncestorOrSelf("HomepageDocTypeAlias");
    

    Use strongly typed content access instead of dynamic

    Your method to get childitems takes a dynamic parameter page. I would have used strongly typed IPublishedContent

    Then you can do you visible check like this

    var childPages = page.Children.Where(x => x.IsVisible()).ToList();
    

    Also you have some redudant checks in this method : childPages.Any() && childPages.Count > 0

    Read this article about the difference between dynamic and stronlgy typed content access : http://24days.in/umbraco/2015/strongly-typed-vs-dynamic-content-access/

    Caching

    For data caching I would have used built in caching from Umbraco : https://our.umbraco.org/documentation/Reference/Cache/updating-cache

    If the menu doesn't need a active state I would probably have used (donut) outputcache only. So it's cached once for the entire website.

    If the navigation needed a active state that would have used both memory and output cache.

    1. Add/Retreive menu structure from cache
    2. Set active state
    3. Output cache so it's only rendered once

    Another thing is also I don't see anything to clear your cache when a editor makes changes in the backend.

    That's my input. Hope you will find it usefull

  • Paul Seal 380 posts 2074 karma points MVP 2x c-trib
    Sep 06, 2016 @ 09:09
    Paul Seal
    0

    Hi Dave Thanks for your detailed reply. I really appreciate it. Regarding your points:

    Use MVC convetions for viewpath Thanks for pointing this out. I tried this in the past but didn't have much success. I put it down to Umbraco's routing being weird. This is clear now, I will do that from now on.

    Query content instead of using path level You are right, I normally use dynamic typing, but wanted to used strongly typed and couldn't find a way to get it to work. Thanks for your example.

    Use strongly typed content access instead of dynamic Again, similar to the last, I couldn't find the visible property to use in a lambda, should have looked for IsVisible. Thanks for this, I will definitely use it.

    I couldn't find the point you were trying to make about the redundant checks.

    Caching

    The nav does need active state. I didn't want to use the built in caching from Umbraco because I wanted to use the simple example for caching that would work outside of Umbraco.

    Thanks again, hopefully someone will see this thread and learn a lot from it.

    Paul

  • Ash 2 posts 72 karma points
    Nov 06, 2018 @ 15:03
    Ash
    0

    Thanks Paul for this interesting blog. Very helpful.

    However, it may sound stupid, but how would you go about adding related links for example home, blogs etc.. using cache implementation for navigation model.

  • Dan Diplo 1459 posts 5623 karma points MVP 3x c-trib
    Nov 06, 2018 @ 23:11
    Dan Diplo
    0

    One small thing I noticed was that you had a redundant check on this property on the NavigationListItem class:

    public bool HasChildren { get { return Items != null && Items.Any() && Items.Count > 0;  } }
    

    You don't need the && Items.Count > 0 after calling Items.Any() as it will never be evaluated and is redundant.

    Also, you can use the built-in Umbraco classes in ApplicationContext.ApplicationCache.RuntimeCache for caching items rather than rolling your own.

Please Sign in or register to post replies

Write your reply to:

Draft