IHasProviderIds.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.ProviderIds == null)
  40. {
  41. return null;
  42. }
  43. string id;
  44. instance.ProviderIds.TryGetValue(name, out id);
  45. return id;
  46. }
  47. /// <summary>
  48. /// Sets a provider id
  49. /// </summary>
  50. /// <param name="instance">The instance.</param>
  51. /// <param name="name">The name.</param>
  52. /// <param name="value">The value.</param>
  53. public static void SetProviderId(this IHasProviderIds instance, string name, string value)
  54. {
  55. // If it's null remove the key from the dictionary
  56. if (string.IsNullOrEmpty(value))
  57. {
  58. if (instance.ProviderIds != null)
  59. {
  60. if (instance.ProviderIds.ContainsKey(name))
  61. {
  62. instance.ProviderIds.Remove(name);
  63. }
  64. }
  65. }
  66. else
  67. {
  68. // Ensure it exists
  69. if (instance.ProviderIds == null)
  70. {
  71. instance.ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  72. }
  73. instance.ProviderIds[name] = value;
  74. }
  75. }
  76. /// <summary>
  77. /// Sets a provider id
  78. /// </summary>
  79. /// <param name="instance">The instance.</param>
  80. /// <param name="provider">The provider.</param>
  81. /// <param name="value">The value.</param>
  82. public static void SetProviderId(this IHasProviderIds instance, MetadataProviders provider, string value)
  83. {
  84. instance.SetProviderId(provider.ToString(), value);
  85. }
  86. }
  87. }