IHasProviderIds.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Model.Entities
  4. {
  5. /// <summary>
  6. /// Since BaseItem and DTOBaseItem both have ProviderIds, this interface helps avoid code repition by using extension methods
  7. /// </summary>
  8. public interface IHasProviderIds
  9. {
  10. /// <summary>
  11. /// Gets or sets the provider ids.
  12. /// </summary>
  13. /// <value>The provider ids.</value>
  14. Dictionary<string, string> ProviderIds { get; set; }
  15. }
  16. /// <summary>
  17. /// Class ProviderIdsExtensions
  18. /// </summary>
  19. public static class ProviderIdsExtensions
  20. {
  21. /// <summary>
  22. /// Determines whether [has provider identifier] [the specified instance].
  23. /// </summary>
  24. /// <param name="instance">The instance.</param>
  25. /// <param name="provider">The provider.</param>
  26. /// <returns><c>true</c> if [has provider identifier] [the specified instance]; otherwise, <c>false</c>.</returns>
  27. public static bool HasProviderId(this IHasProviderIds instance, MetadataProviders provider)
  28. {
  29. return !string.IsNullOrEmpty(instance.GetProviderId(provider.ToString()));
  30. }
  31. /// <summary>
  32. /// Gets a provider id
  33. /// </summary>
  34. /// <param name="instance">The instance.</param>
  35. /// <param name="provider">The provider.</param>
  36. /// <returns>System.String.</returns>
  37. public static string GetProviderId(this IHasProviderIds instance, MetadataProviders provider)
  38. {
  39. return instance.GetProviderId(provider.ToString());
  40. }
  41. /// <summary>
  42. /// Gets a provider id
  43. /// </summary>
  44. /// <param name="instance">The instance.</param>
  45. /// <param name="name">The name.</param>
  46. /// <returns>System.String.</returns>
  47. public static string GetProviderId(this IHasProviderIds instance, string name)
  48. {
  49. if (instance == null)
  50. {
  51. throw new ArgumentNullException("instance");
  52. }
  53. if (instance.ProviderIds == null)
  54. {
  55. return null;
  56. }
  57. string id;
  58. instance.ProviderIds.TryGetValue(name, out id);
  59. return id;
  60. }
  61. /// <summary>
  62. /// Sets a provider id
  63. /// </summary>
  64. /// <param name="instance">The instance.</param>
  65. /// <param name="name">The name.</param>
  66. /// <param name="value">The value.</param>
  67. public static void SetProviderId(this IHasProviderIds instance, string name, string value)
  68. {
  69. if (instance == null)
  70. {
  71. throw new ArgumentNullException("instance");
  72. }
  73. // If it's null remove the key from the dictionary
  74. if (string.IsNullOrEmpty(value))
  75. {
  76. if (instance.ProviderIds != null)
  77. {
  78. if (instance.ProviderIds.ContainsKey(name))
  79. {
  80. instance.ProviderIds.Remove(name);
  81. }
  82. }
  83. }
  84. else
  85. {
  86. // Ensure it exists
  87. if (instance.ProviderIds == null)
  88. {
  89. instance.ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  90. }
  91. instance.ProviderIds[name] = value;
  92. }
  93. }
  94. /// <summary>
  95. /// Sets a provider id
  96. /// </summary>
  97. /// <param name="instance">The instance.</param>
  98. /// <param name="provider">The provider.</param>
  99. /// <param name="value">The value.</param>
  100. public static void SetProviderId(this IHasProviderIds instance, MetadataProviders provider, string value)
  101. {
  102. instance.SetProviderId(provider.ToString(), value);
  103. }
  104. }
  105. }