Kentico CMS helper functions to query nodes and return strong typed objects

Wed Oct 16 2013

The following post shows Kentico 7 CMS wrapper functions that allow you to easily query nodes and return back a strong type.

The functionality here is using the mapping functionality shown in this post: Kentico CMS Mapper functions using Reflection in C# .NET.

GetListByClass - this function returns back a list of nodes mapped to the requested type with the given options: (Please see below regarding caching)

// function that returns a strong typed list by class and caches by default
public static List GetListByClass(string className, string path, int levels, int limit)
    where T : BaseModel, new()
{
    return GetListByClass(className, path, levels, limit, true);
}

// function that a strong typed list by class
public static List GetListByClass(string className, string path, int levels, int limit, bool cache)
    where T : BaseModel, new()
{
    // define function
    Func> fn = delegate()
    {
        // get a tree provider
        var th = new TreeProvider();

        // find the node
        var nodes = th.SelectNodes(
            CMSContext.CurrentSiteName, path,
            Globals.CultureCode, false,
            className, "", "NodeOrder", levels, true, limit);

        // return the list            
        return Mapper.MapToList(nodes).OrderBy(x => x.Document.NodeOrder).ToList();
    };

    // check if 
    if (cache)
    {
        // define cache key
        string cacheKey = String.Concat("GetList", "|", path, "|", className, "|", levels, "|", limit);

        // return the item from the cahc if exists
        return CacheManager.Get>(cacheKey, fn);
    }

    // else return the live data
    return fn();
}

GetListByClasses - this function takes a list of classes to filter

public static List GetListByClasses(List className, string path, int levels, int limit)
    where T : BaseModel, new()
{
    // join the filter list, return the list
    return GetListByClass(String.Join(";", className), path, levels, limit);
}

GetSingleByClass - this function returns a single object

// function that returns a single strong type by class
public static T GetSingleByClass(string className, string path, int levels)
    where T : BaseModel, new()
{
    // return the first item
    var itm = GetListByClass(className, path, levels, 1).FirstOrDefault();

    // check if null
    if (itm == null)
        itm = new T();

    // return;
    return itm;
}

The functions above use the Caching class below. I found this example from http://codepm.wordpress.com/2010/09/28/using-c-delegates-with-asp-net-data-caching/

// http://codepm.wordpress.com/2010/09/28/using-c-delegates-with-asp-net-data-caching/
public static class CacheManager
{
    // define default timeout
    static double CacheTimeoutMinutes = 20;

    // function that returns the full key
    private static string FullKey(string key)
    {
        return String.Concat("MySite.", key);
    }

    // function that added the object to the cache
    private static void Add(object cacheObject, string key, double timeoutminutes)
    {
        HttpContext.Current.Cache.Insert(FullKey(key), cacheObject, 
            null, 
            DateTime.MaxValue,
            TimeSpan.FromMinutes(timeoutminutes),
            CacheItemPriority.Normal, 
            null);
    }

    // function that returns a single item by key
    public static T Get(string key) where T : class
    {
        return HttpContext.Current.Cache[FullKey(key)] as T;
    }

    // function that returns tries to get the cached item with default time
    public static T Get(string key, Func fn) where T : class
    {
        return Get(key, CacheTimeoutMinutes, fn);
    }

    // function that returns tries to get the cached item with the given time
    public static T Get(string key, double timeoutminutes, Func fn) where T : class
    {        
        // try to get the item
        var obj = Get(key);
        if (obj == default(T))
        {         
            // if fails, execute function to get the data
            obj = fn();

            // add to the cache
            Add(obj, key, timeoutminutes);
        }

        // return the item
        return obj;
    }
}