I'm working on a recommendation algorithm which all works fine. But now I wanted to implement this code into the branch of my development team.
I'll start from the top. My algorithm can recommend 2 types of objects, restaurants and dishes.
Restaurant:
public class Restaurant
{
public Guid Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public List<Tag> Tags { get; set; } = new List<Tag>();
public int PriceRange { get; set; }
}
And dish:
public class Dish
{
public Guid Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public virtual Restaurant rest { get; set; }
[ForeignKey("rest")]
public Guid RestaurantId { get; set; }
public List<Tag> Tags { get; set; }
}
Now my product owner wants the list to be like this when it's being presented on the home page of our app:
[Restaurant][Dish][Restaurant][Dish] Etc...
So basically, he wants to alternate the type of object that's being recommended. These dishes and restaurants are completely separate. They are generated by my algorithm purely on the user's preferences and have no correlation with eachother at all.
Now my problem is how to return such a list. I figured I'd need a wrapper class which contains either a Restaurant
or Dish
like this:
public class RecommenderItem
{
public Restaurant rest { get; set; }
public Dish dish { get; set; }
}
This way I can create a List<RecommenderItem>
and return that to the client. The client would only need to check which attribute is null and retrieve the values from the one that is not.
I'm just unsure if this is the correct approach. Are there any 'best practices' in doing this? Let me know if I should elaborate more!
Id
andName
(and e.g.Tags
) and create a list of that interface (the interface could also expose a 'Type
' for the alternation) – CorianderRecommenderItem
sounds like exactly the right solution to me, since it appears that it's a list of dishes, each of which is associated with a restaurant. Your return type should reflect that, as it does. – AzoicRestaurantItem
. EveryRestaurantItem
is a tuple of a Dish, and the Restaurant where that dish is served. At least that's what I inferred from what you're saying. My understanding is that this is not a list where any given element could randomly be either a restaurant or a dish, but rather a list of restaurant-dish pairs. – AzoicList<Object>
where anything could be in there -- unless the UI lets you use some kind of data template mechanism, where it'll dynamically choose the right template to display the item. Then go withList<Object>
, orList<WhateverBaseClass>
. I'd change your example to[Restaurant][Dish][Restaurant][Restaurant][Dish]
or something to make that clear. (OTOH nobody but me missed your point, so maybe you're fine!) – Azoic[Restaurant][Dish][Restaurant][Dish]
, which is exactly what my product owner wants. I'll add a description to explain it better. – Melo