Filling list with different types of objects

女生的网名这么多〃 提交于 2020-01-02 07:42:11

问题


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!


回答1:


If they doesn't have common base class then creating one wrapper class is the best solution. At the same time you can be more flexible and create something like

public class RecommendationItem
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string PageUrl { get; set; }
    public object Entity { get; set; }
}

So you can include all common information in this class and client will not be required to check with which object type he works. In such case it would be easier to add one more item type. At the same type I added reference to entity itself - it can be used if some specific handling for one or two item types is required.




回答2:


You can declare an interface IRecommenderItem:

public interface IRecommenderItem
{
    //shared properties
}

public class Restaurant : IRecommenderItem
{

}

public class Dish : IRecommenderItem
{

}

than, you can type:

List<IRecommenderItem> m = new List<IRecommenderItem>();



回答3:


If you are going to connect pairs of elements it always makes sense to me to... well, pair the elements. I am assuming that each dish is specific to a particular restaurant? So the list would be [Restaurant1][Dish for Restaurant1][Restaurant2][Dish for Restaurant2]...?

I like the previous answer by oryol creating a common base class as well.

So, your RecommenderItem class is fine. But fill in both properties and pass a list of pairs back. Expand the list into the full set of items for display by creating a new List, iterating through the list of RecommenderItems and adding Restaurant and Dish from each entry in it.



来源:https://stackoverflow.com/questions/41124085/filling-list-with-different-types-of-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!