jellyfin/MediaBrowser.Model/Entities/IHasProviderIds.cs

105 lines
3.3 KiB
C#
Raw Normal View History

2013-02-20 18:33:05 -07:00
using System;
using System.Collections.Generic;
namespace MediaBrowser.Model.Entities
{
/// <summary>
/// Since BaseItem and DTOBaseItem both have ProviderIds, this interface helps avoid code repition by using extension methods
/// </summary>
public interface IHasProviderIds
{
/// <summary>
/// Gets or sets the provider ids.
/// </summary>
/// <value>The provider ids.</value>
Dictionary<string, string> ProviderIds { get; set; }
}
/// <summary>
/// Class ProviderIdsExtensions
/// </summary>
public static class ProviderIdsExtensions
{
/// <summary>
/// Gets a provider id
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <returns>System.String.</returns>
public static string GetProviderId(this IHasProviderIds instance, MetadataProviders provider)
{
return instance.GetProviderId(provider.ToString());
}
/// <summary>
/// Gets a provider id
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="name">The name.</param>
/// <returns>System.String.</returns>
public static string GetProviderId(this IHasProviderIds instance, string name)
{
2013-06-17 13:35:43 -07:00
if (instance == null)
{
throw new ArgumentNullException("instance");
}
2013-02-20 18:33:05 -07:00
if (instance.ProviderIds == null)
{
return null;
}
string id;
instance.ProviderIds.TryGetValue(name, out id);
return id;
}
/// <summary>
/// Sets a provider id
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
public static void SetProviderId(this IHasProviderIds instance, string name, string value)
{
2013-06-17 13:35:43 -07:00
if (instance == null)
{
throw new ArgumentNullException("instance");
}
2013-02-20 18:33:05 -07:00
// If it's null remove the key from the dictionary
if (string.IsNullOrEmpty(value))
{
if (instance.ProviderIds != null)
{
if (instance.ProviderIds.ContainsKey(name))
{
instance.ProviderIds.Remove(name);
}
}
}
else
{
// Ensure it exists
if (instance.ProviderIds == null)
{
instance.ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
instance.ProviderIds[name] = value;
}
}
/// <summary>
/// Sets a provider id
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <param name="value">The value.</param>
public static void SetProviderId(this IHasProviderIds instance, MetadataProviders provider, string value)
{
instance.SetProviderId(provider.ToString(), value);
}
}
}