Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RecommenderContentItem
- {
- public Guid Id { get; set; }
- public List<Tag> Tags { get; set; } //A tag is an object which only contains an Id and value, not relevant to the question.
- public double AverageRating { get; set; }
- }
- public static List<Guid> FilterOnContent<T>(List<T> ratedItems,
- List<T> itemsToFilter)
- {
- var cnv = new RciConverter();
- var ratedRcis = new List<RecommenderContentItem>();
- var toFilterRcis = new List<RecommenderContentItem>();
- if (typeof(T) == typeof(Restaurant))
- {
- var rated = (IEnumerable<Restaurant>)ratedItems;
- var toFilter = (IEnumerable<Restaurant>)itemsToFilter;
- ratedRcis = cnv.ConvertMany(rated).ToList();
- toFilterRcis = cnv.ConvertMany(toFilter).ToList();
- }
- if (typeof(T) == typeof(AlgorithmRestaurant))
- {
- var rated = (IEnumerable<AlgorithmRestaurant>)ratedItems;
- var toFilter = (IEnumerable<AlgorithmRestaurant>)itemsToFilter;
- ratedRcis = cnv.ConvertMany(rated).ToList();
- toFilterRcis = cnv.ConvertMany(toFilter).ToList();
- }
- if (typeof(T) == typeof(Dish))
- {
- var rated = (IEnumerable<Dish>)ratedItems;
- var toFilter = (IEnumerable<Dish>)itemsToFilter;
- ratedRcis = cnv.ConvertMany(rated).ToList();
- toFilterRcis = cnv.ConvertMany(toFilter).ToList();
- }
- if (!ratedRcis.Any() || !toFilterRcis.Any())
- throw new TypeArgumentException("Invalid type."); //Custom exception written by John Skeet.
- return ContentBasedFilter.Filter(ratedRcis, toFilterRcis).Select(rci => rci.Id).ToList();
- }
- public class RciConverter : IConverter<Restaurant, RecommenderContentItem>,
- IConverter<AlgorithmRestaurant, RecommenderContentItem>,
- IConverter<Dish, RecommenderContentItem>
- {
- //All 6 methods for converting/mapping here...
- }
- public interface IConverter<in TSource, out TDestination>
- where TSource : class
- where TDestination : class
- {
- IEnumerable<TDestination> ConvertMany(IEnumerable<TSource> sourceObjects);
- TDestination Convert(TSource sourceObject);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement