Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. using System;
  2. using Umbraco.Core;
  3. using Umbraco.Core.Models;
  4. using Umbraco.Web.PublishedCache;
  5.  
  6. public static class UmbracoGuidExtensions {
  7.  
  8. /// <summary>
  9. /// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not
  10. /// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID
  11. /// has been requested before).
  12. /// </summary>
  13. /// <param name="cache">A reference to the content cache.</param>
  14. /// <param name="guid">The GUID of the content.</param>
  15. /// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns>
  16. public static IPublishedContent GetByGuid(this ContextualPublishedContentCache cache, string guid) {
  17. return GetByGuid(cache, Guid.Parse(guid));
  18. }
  19.  
  20. /// <summary>
  21. /// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not
  22. /// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID
  23. /// has been requested before).
  24. /// </summary>
  25. /// <param name="cache">A reference to the content cache.</param>
  26. /// <param name="guid">The GUID of the content.</param>
  27. /// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns>
  28. public static IPublishedContent GetByGuid(this ContextualPublishedContentCache cache, Guid guid) {
  29.  
  30. // Get the ID from the database (or runtime cache)
  31. Attempt<int> attempt = ApplicationContext.Current.Services.EntityService.GetIdForKey(guid, UmbracoObjectTypes.Media);
  32.  
  33. // Find the media in the media cache
  34. return attempt.Success ? cache.GetById(attempt.Result) : null;
  35.  
  36. }
  37.  
  38. /// <summary>
  39. /// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not
  40. /// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID
  41. /// has been requested before).
  42. /// </summary>
  43. /// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam>
  44. /// <param name="cache">A reference to the content cache.</param>
  45. /// <param name="guid">The GUID of the content.</param>
  46. /// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param>
  47. /// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns>
  48. public static T GetByGuid<T>(this ContextualPublishedContentCache cache, string guid, Func<IPublishedContent, T> callback) {
  49. return callback(GetByGuid(cache, guid));
  50. }
  51.  
  52. /// <summary>
  53. /// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not
  54. /// available in the content cache, so a lookup will be made in the database (or the runtime cache if the GUID
  55. /// has been requested before).
  56. /// </summary>
  57. /// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam>
  58. /// <param name="cache">A reference to the content cache.</param>
  59. /// <param name="guid">The GUID of the content.</param>
  60. /// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param>
  61. /// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns>
  62. public static T GetByGuid<T>(this ContextualPublishedContentCache cache, Guid guid, Func<IPublishedContent, T> callback) {
  63. return callback(GetByGuid(cache, guid));
  64. }
  65.  
  66. /// <summary>
  67. /// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not
  68. /// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID
  69. /// has been requested before).
  70. /// </summary>
  71. /// <param name="cache">A reference to the media cache.</param>
  72. /// <param name="guid">The GUID of the media.</param>
  73. /// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns>
  74. public static IPublishedContent GetByGuid(this ContextualPublishedMediaCache cache, string guid) {
  75. return GetByGuid(cache, Guid.Parse(guid));
  76. }
  77.  
  78. /// <summary>
  79. /// Gets an instance of <code>IPublishedContent</code> based on the specified <code>guid</code>. GUIDs are not
  80. /// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID
  81. /// has been requested before).
  82. /// </summary>
  83. /// <param name="cache">A reference to the media cache.</param>
  84. /// <param name="guid">The GUID of the media.</param>
  85. /// <returns>Returns an instance of <code>IPublishedContent</code> if found, otherwise <code>null</code>.</returns>
  86. public static IPublishedContent GetByGuid(this ContextualPublishedMediaCache cache, Guid guid) {
  87.  
  88. // Get the ID from the database (or runtime cache)
  89. Attempt<int> attempt = ApplicationContext.Current.Services.EntityService.GetIdForKey(guid, UmbracoObjectTypes.Media);
  90.  
  91. // Find the media in the media cache
  92. return attempt.Success ? cache.GetById(attempt.Result) : null;
  93.  
  94. }
  95.  
  96. /// <summary>
  97. /// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not
  98. /// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID
  99. /// has been requested before).
  100. /// </summary>
  101. /// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam>
  102. /// <param name="cache">A reference to the media cache.</param>
  103. /// <param name="guid">The GUID of the media.</param>
  104. /// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param>
  105. /// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns>
  106. public static T GetByGuid<T>(this ContextualPublishedMediaCache cache, string guid, Func<IPublishedContent, T> callback) {
  107. return callback(GetByGuid(cache, guid));
  108. }
  109.  
  110. /// <summary>
  111. /// Gets an instance of <code>T</code> based on the <code>IPublishedContent</code> with the specified <code>guid</code>. GUIDs are not
  112. /// available in the media cache, so a lookup will be made in the database (or the runtime cache if the GUID
  113. /// has been requested before).
  114. /// </summary>
  115. /// <typeparam name="T">The type returned by the specified <code>callback</code>.</typeparam>
  116. /// <param name="cache">A reference to the media cache.</param>
  117. /// <param name="guid">The GUID of the media.</param>
  118. /// <param name="callback">The callback function used for parsing the found instance of <code>IPublishedContent</code>.</param>
  119. /// <returns>Returns an instance of <code>T</code> based on the specified <code>callback</code>.</returns>
  120. public static T GetByGuid<T>(this ContextualPublishedMediaCache cache, Guid guid, Func<IPublishedContent, T> callback) {
  121. return callback(GetByGuid(cache, guid));
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement