Created
July 15, 2015 17:56
-
-
Save abjerner/0a8eae7346f9b1c79908 to your computer and use it in GitHub Desktop.
UmbracoGuidExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
using Umbraco.Web.PublishedCache; | |
public static class UmbracoGuidExtensions { | |
/// <summary> | |
/// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not | |
/// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <param name="cache">A reference to the content cache.</param> | |
/// <param name="guid">The GUID of the content.</param> | |
/// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns> | |
public static IPublishedContent GetByGuid(this ContextualPublishedContentCache cache, string guid) { | |
return GetByGuid(cache, Guid.Parse(guid)); | |
} | |
/// <summary> | |
/// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not | |
/// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <param name="cache">A reference to the content cache.</param> | |
/// <param name="guid">The GUID of the content.</param> | |
/// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns> | |
public static IPublishedContent GetByGuid(this ContextualPublishedContentCache cache, Guid guid) { | |
// Get the ID from the database (or runtime cache) | |
Attempt<int> attempt = ApplicationContext.Current.Services.EntityService.GetIdForKey(guid, UmbracoObjectTypes.Media); | |
// Find the media in the media cache | |
return attempt.Success ? cache.GetById(attempt.Result) : null; | |
} | |
/// <summary> | |
/// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not | |
/// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam> | |
/// <param name="cache">A reference to the content cache.</param> | |
/// <param name="guid">The GUID of the content.</param> | |
/// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param> | |
/// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns> | |
public static T GetByGuid<T>(this ContextualPublishedContentCache cache, string guid, Func<IPublishedContent, T> callback) { | |
return callback(GetByGuid(cache, guid)); | |
} | |
/// <summary> | |
/// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not | |
/// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam> | |
/// <param name="cache">A reference to the content cache.</param> | |
/// <param name="guid">The GUID of the content.</param> | |
/// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param> | |
/// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns> | |
public static T GetByGuid<T>(this ContextualPublishedContentCache cache, Guid guid, Func<IPublishedContent, T> callback) { | |
return callback(GetByGuid(cache, guid)); | |
} | |
/// <summary> | |
/// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not | |
/// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <param name="cache">A reference to the media cache.</param> | |
/// <param name="guid">The GUID of the media.</param> | |
/// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns> | |
public static IPublishedContent GetByGuid(this ContextualPublishedMediaCache cache, string guid) { | |
return GetByGuid(cache, Guid.Parse(guid)); | |
} | |
/// <summary> | |
/// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not | |
/// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <param name="cache">A reference to the media cache.</param> | |
/// <param name="guid">The GUID of the media.</param> | |
/// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns> | |
public static IPublishedContent GetByGuid(this ContextualPublishedMediaCache cache, Guid guid) { | |
// Get the ID from the database (or runtime cache) | |
Attempt<int> attempt = ApplicationContext.Current.Services.EntityService.GetIdForKey(guid, UmbracoObjectTypes.Media); | |
// Find the media in the media cache | |
return attempt.Success ? cache.GetById(attempt.Result) : null; | |
} | |
/// <summary> | |
/// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not | |
/// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam> | |
/// <param name="cache">A reference to the media cache.</param> | |
/// <param name="guid">The GUID of the media.</param> | |
/// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param> | |
/// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns> | |
public static T GetByGuid<T>(this ContextualPublishedMediaCache cache, string guid, Func<IPublishedContent, T> callback) { | |
return callback(GetByGuid(cache, guid)); | |
} | |
/// <summary> | |
/// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not | |
/// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID | |
/// has been requested before). | |
/// </summary> | |
/// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam> | |
/// <param name="cache">A reference to the media cache.</param> | |
/// <param name="guid">The GUID of the media.</param> | |
/// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param> | |
/// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns> | |
public static T GetByGuid<T>(this ContextualPublishedMediaCache cache, Guid guid, Func<IPublishedContent, T> callback) { | |
return callback(GetByGuid(cache, guid)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment