IHasProviderIds.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /// Gets a provider id
  23. /// </summary>
  24. /// <param name="instance">The instance.</param>
  25. /// <param name="provider">The provider.</param>
  26. /// <returns>System.String.</returns>
  27. public static string GetProviderId(this IHasProviderIds instance, MetadataProviders provider)
  28. {
  29. return instance.GetProviderId(provider.ToString());
  30. }
  31. /// <summary>
  32. /// Gets a provider id
  33. /// </summary>
  34. /// <param name="instance">The instance.</param>
  35. /// <param name="name">The name.</param>
  36. /// <returns>System.String.</returns>
  37. public static string GetProviderId(this IHasProviderIds instance, string name)
  38. {
  39. if (instance == null)
  40. {
  41. throw new ArgumentNullException("instance");
  42. }
  43. if (instance.ProviderIds == null)
  44. {
  45. return null;
  46. }
  47. string id;
  48. instance.ProviderIds.TryGetValue(name, out id);
  49. return id;
  50. }
  51. /// <summary>
  52. /// Sets a provider id
  53. /// </summary>
  54. /// <param name="instance">The instance.</param>
  55. /// <param name="name">The name.</param>
  56. /// <param name="value">The value.</param>
  57. public static void SetProviderId(this IHasProviderIds instance, string name, string value)
  58. {
  59. if (instance == null)
  60. {
  61. throw new ArgumentNullException("instance");
  62. }
  63. // If it's null remove the key from the dictionary
  64. if (string.IsNullOrEmpty(value))
  65. {
  66. if (instance.ProviderIds != null)
  67. {
  68. if (instance.ProviderIds.ContainsKey(name))
  69. {
  70. instance.ProviderIds.Remove(name);
  71. }
  72. }
  73. }
  74. else
  75. {
  76. // Ensure it exists
  77. if (instance.ProviderIds == null)
  78. {
  79. instance.ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  80. }
  81. instance.ProviderIds[name] = value;
  82. }
  83. }
  84. /// <summary>
  85. /// Sets a provider id
  86. /// </summary>
  87. /// <param name="instance">The instance.</param>
  88. /// <param name="provider">The provider.</param>
  89. /// <param name="value">The value.</param>
  90. public static void SetProviderId(this IHasProviderIds instance, MetadataProviders provider, string value)
  91. {
  92. instance.SetProviderId(provider.ToString(), value);
  93. }
  94. }
  95. }