2
0

ProviderIdsExtensions.cs 3.5 KB

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