用不同类型的对象填充列表 [英] Filling list with different types of objects

查看:28
本文介绍了用不同类型的对象填充列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种一切正常的推荐算法.但现在我想将这段代码实现到我的开发团队的分支中.

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.

我将从顶部开始.我的算法可以推荐 2 种类型的对象,餐厅和菜肴.

I'll start from the top. My algorithm can recommend 2 types of objects, restaurants and dishes.

餐厅:

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; }
}

和菜:

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.

现在我的问题是如何返回这样一个列表.我想我需要一个包含 RestaurantDish 的包装类,如下所示:

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; }
}

通过这种方式,我可以创建一个 List 并将其返回给客户端.客户端只需要检查哪个属性为空并从不是的那个中检索值.

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!

推荐答案

如果他们没有共同的基类,那么创建一个包装类是最好的解决方案.同时,您可以更灵活地创建类似

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.

这篇关于用不同类型的对象填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆