using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using NzbDrone.Api.REST; using NzbDrone.Common.Cache; using NzbDrone.Core.Datastore; namespace NzbDrone.Api.Extensions { public static class LazyExtensions { private static readonly ICached SetterCache = new Cached(); public static IEnumerable LoadSubtype(this IEnumerable parents, Func foreignKeySelector, IBasicRepository childRepository) where TChild : ModelBase, new() where TParent : RestResource { var parentList = parents.Where(p => foreignKeySelector(p) != 0).ToList(); if (!parentList.Any()) { return parents; } var ids = parentList.Select(foreignKeySelector).Distinct(); var childDictionary = childRepository.Get(ids).ToDictionary(child => child.Id, child => child); var childSetter = GetChildSetter(); foreach (var episode in parentList) { childSetter.Invoke(episode, new object[] { childDictionary[foreignKeySelector(episode)] }); } return parents; } private static MethodInfo GetChildSetter() where TChild : ModelBase where TParent : RestResource { var key = typeof(TChild).FullName + typeof(TParent).FullName; return SetterCache.Get(key, () => { var property = typeof(TParent).GetProperties().Single(c => c.PropertyType == typeof(TChild)); return property.GetSetMethod(); }); } } }